From ac4ff7037d4669548d3b670c2f776db0a8046a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 28 Dec 2019 11:39:53 +0100 Subject: [PATCH 1/2] :bug: Fix testing CliRunner --- typer/testing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/typer/testing.py b/typer/testing.py index 1a07c43f2b..88cd83dc6b 100644 --- a/typer/testing.py +++ b/typer/testing.py @@ -8,7 +8,6 @@ class CliRunner(ClickCliRunner): def invoke( # type: ignore self, app: Typer, - cli: Typer, args: Optional[Union[str, Iterable[str]]] = None, input: Optional[Union[bytes, Text, IO[Any]]] = None, env: Optional[Mapping[str, str]] = None, From 45f995c6aa8181e136acf9eb02783c01190131ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 28 Dec 2019 13:57:56 +0100 Subject: [PATCH 2/2] :white_check_mark: Add tests for the first steps --- scripts/test.sh | 2 + .../test_first_steps/test_tutorial001.py | 16 +++++- .../test_first_steps/test_tutorial002.py | 33 ++++++++++++ .../test_first_steps/test_tutorial003.py | 33 ++++++++++++ .../test_first_steps/test_tutorial004.py | 45 ++++++++++++++++ .../test_first_steps/test_tutorial005.py | 52 +++++++++++++++++++ 6 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 tests/test_tutorial/test_first_steps/test_tutorial002.py create mode 100644 tests/test_tutorial/test_first_steps/test_tutorial003.py create mode 100644 tests/test_tutorial/test_first_steps/test_tutorial004.py create mode 100644 tests/test_tutorial/test_first_steps/test_tutorial005.py diff --git a/scripts/test.sh b/scripts/test.sh index 6edfebf6c3..5440f2ce2e 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -6,3 +6,5 @@ set -x export PYTHONPATH=./docs/src pytest --cov=typer --cov=tests --cov=docs/src --cov-report=term-missing ${@} bash ./scripts/lint.sh +# Check README.md is up to date +diff --brief docs/index.md README.md diff --git a/tests/test_tutorial/test_first_steps/test_tutorial001.py b/tests/test_tutorial/test_first_steps/test_tutorial001.py index cd638359e4..d7b8473618 100644 --- a/tests/test_tutorial/test_first_steps/test_tutorial001.py +++ b/tests/test_tutorial/test_first_steps/test_tutorial001.py @@ -1,13 +1,25 @@ +import subprocess + import typer from typer.testing import CliRunner -from first_steps.tutorial001 import main +from first_steps import tutorial001 as mod runner = CliRunner() def test_cli(): app = typer.Typer() - app.command()(main) + app.command()(mod.main) result = runner.invoke(app, []) assert result.output == "Hello World\n" + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial002.py b/tests/test_tutorial/test_first_steps/test_tutorial002.py new file mode 100644 index 0000000000..9c70a3e577 --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial002.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial002 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, []) + assert result.exit_code != 0 + assert 'Error: Missing argument "NAME"' in result.output + + +def test_2(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial003.py b/tests/test_tutorial/test_first_steps/test_tutorial003.py new file mode 100644 index 0000000000..bf0038dadf --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial003.py @@ -0,0 +1,33 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial003 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code != 0 + assert 'Error: Missing argument "LASTNAME"' in result.output + + +def test_2(): + result = runner.invoke(app, ["Camila", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial004.py b/tests/test_tutorial/test_first_steps/test_tutorial004.py new file mode 100644 index 0000000000..e00f5a2041 --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial004.py @@ -0,0 +1,45 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial004 as mod + +runner = CliRunner() + +app = typer.Typer() +app.command()(mod.main) + + +def test_1(): + result = runner.invoke(app, ["Camila", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_formal_1(): + result = runner.invoke(app, ["Camila", "Gutiérrez", "--formal"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_formal_2(): + result = runner.invoke(app, ["Camila", "--formal", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_formal_3(): + result = runner.invoke(app, ["--formal", "Camila", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout diff --git a/tests/test_tutorial/test_first_steps/test_tutorial005.py b/tests/test_tutorial/test_first_steps/test_tutorial005.py new file mode 100644 index 0000000000..4aa2e722e5 --- /dev/null +++ b/tests/test_tutorial/test_first_steps/test_tutorial005.py @@ -0,0 +1,52 @@ +import subprocess + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial005 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 "--lastname TEXT" in result.output + assert "--formal / --no-formal" in result.output + + +def test_1(): + result = runner.invoke(app, ["Camila"]) + assert result.exit_code == 0 + assert "Hello Camila" in result.output + + +def test_option_lastname(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_option_lastname_2(): + result = runner.invoke(app, ["--lastname", "Gutiérrez", "Camila"]) + assert result.exit_code == 0 + assert "Hello Camila Gutiérrez" in result.output + + +def test_formal_1(): + result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"]) + assert result.exit_code == 0 + assert "Good day Ms. Camila Gutiérrez." in result.output + + +def test_script(): + result = subprocess.run( + ["coverage", "run", "--parallel-mode", mod.__file__, "--help"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + assert "Usage" in result.stdout