From 3332ab3cae5c760f7a9f61c3859c4500bf6ed638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 29 Dec 2019 17:09:56 +0100 Subject: [PATCH] :white_check_mark: Add tests for CLI arguments --- .../test_tutorial/test_arguments/__init__.py | 0 .../test_arguments/test_tutorial001.py | 33 ++++++++++++++++ .../test_arguments/test_tutorial002.py | 39 +++++++++++++++++++ .../test_arguments/test_tutorial003.py | 39 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 tests/test_tutorial/test_arguments/__init__.py create mode 100644 tests/test_tutorial/test_arguments/test_tutorial001.py create mode 100644 tests/test_tutorial/test_arguments/test_tutorial002.py create mode 100644 tests/test_tutorial/test_arguments/test_tutorial003.py diff --git a/tests/test_tutorial/test_arguments/__init__.py b/tests/test_tutorial/test_arguments/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tutorial/test_arguments/test_tutorial001.py b/tests/test_tutorial/test_arguments/test_tutorial001.py new file mode 100644 index 0000000..668e657 --- /dev/null +++ b/tests/test_tutorial/test_arguments/test_tutorial001.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from arguments import tutorial001 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_call_no_arg(): + result = runner.invoke(app) + assert result.exit_code != 0 + assert 'Error: Missing argument "NAME".' in result.output + + +def test_call_arg(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_arguments/test_tutorial002.py b/tests/test_tutorial/test_arguments/test_tutorial002.py new file mode 100644 index 0000000..82c6198 --- /dev/null +++ b/tests/test_tutorial/test_arguments/test_tutorial002.py @@ -0,0 +1,39 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from arguments import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "[OPTIONS] [NAME]" in result.output + + +def test_call_no_arg(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "Hello World!" in result.output + + +def test_call_arg(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_arguments/test_tutorial003.py b/tests/test_tutorial/test_arguments/test_tutorial003.py new file mode 100644 index 0000000..255b793 --- /dev/null +++ b/tests/test_tutorial/test_arguments/test_tutorial003.py @@ -0,0 +1,39 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from arguments import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_help(): + result = runner.invoke(app, ["--help"]) + assert result.exit_code == 0 + assert "[OPTIONS] [NAME]" in result.output + + +def test_call_no_arg(): + result = runner.invoke(app) + assert result.exit_code == 0 + assert "Hello Wade Wilson" in result.output + + +def test_call_arg(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout