✅ Add tests for the first steps
This commit is contained in:
parent
ac4ff7037d
commit
45f995c6aa
6 changed files with 179 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
tests/test_tutorial/test_first_steps/test_tutorial002.py
Normal file
33
tests/test_tutorial/test_first_steps/test_tutorial002.py
Normal file
|
@ -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
tests/test_tutorial/test_first_steps/test_tutorial003.py
Normal file
33
tests/test_tutorial/test_first_steps/test_tutorial003.py
Normal file
|
@ -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
tests/test_tutorial/test_first_steps/test_tutorial004.py
Normal file
45
tests/test_tutorial/test_first_steps/test_tutorial004.py
Normal file
|
@ -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
tests/test_tutorial/test_first_steps/test_tutorial005.py
Normal file
52
tests/test_tutorial/test_first_steps/test_tutorial005.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue