Skip to content

Commit

Permalink
✅ Fix testing, add tests
Browse files Browse the repository at this point in the history
Fix testing tools, add first tests
  • Loading branch information
tiangolo authored Dec 28, 2019
2 parents 39e034e + 45f995c commit c92fddc
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 3 deletions.
2 changes: 2 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 14 additions & 2 deletions tests/test_tutorial/test_first_steps/test_tutorial001.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions tests/test_tutorial/test_first_steps/test_tutorial002.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions tests/test_tutorial/test_first_steps/test_tutorial003.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions tests/test_tutorial/test_first_steps/test_tutorial004.py
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions tests/test_tutorial/test_first_steps/test_tutorial005.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion typer/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c92fddc

Please sign in to comment.