✅ Add tests for CLI arguments
This commit is contained in:
parent
ce1316ce8f
commit
3332ab3cae
4 changed files with 111 additions and 0 deletions
0
tests/test_tutorial/test_arguments/__init__.py
Normal file
0
tests/test_tutorial/test_arguments/__init__.py
Normal file
33
tests/test_tutorial/test_arguments/test_tutorial001.py
Normal file
33
tests/test_tutorial/test_arguments/test_tutorial001.py
Normal file
|
@ -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
|
39
tests/test_tutorial/test_arguments/test_tutorial002.py
Normal file
39
tests/test_tutorial/test_arguments/test_tutorial002.py
Normal file
|
@ -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
|
39
tests/test_tutorial/test_arguments/test_tutorial003.py
Normal file
39
tests/test_tutorial/test_arguments/test_tutorial003.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue