Add tests for commands: callbacks

This commit is contained in:
Sebastián Ramírez 2020-01-05 21:38:57 +01:00
parent 56bb2576c4
commit d0ee82ba82
8 changed files with 212 additions and 0 deletions

View file

@ -0,0 +1,61 @@
import subprocess
from commands.callback import tutorial001 as mod
from typer.testing import CliRunner
app = mod.app
runner = CliRunner()
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Manage users in the awesome CLI app." in result.output
assert "--verbose / --no-verbose" in result.output
def test_create():
result = runner.invoke(app, ["create", "Camila"])
assert result.exit_code == 0
assert "Creating user: Camila" in result.output
def test_create_verbose():
result = runner.invoke(app, ["--verbose", "create", "Camila"])
assert result.exit_code == 0
assert "Will write verbose output" in result.output
assert "About to create a user" in result.output
assert "Creating user: Camila" in result.output
assert "Just created a user" in result.output
def test_delete():
result = runner.invoke(app, ["delete", "Camila"])
assert result.exit_code == 0
assert "Deleting user: Camila" in result.output
def test_delete_verbose():
result = runner.invoke(app, ["--verbose", "delete", "Camila"])
assert result.exit_code == 0
assert "Will write verbose output" in result.output
assert "About to delete a user" in result.output
assert "Deleting user: Camila" in result.output
assert "Just deleted a user" in result.output
def test_wrong_verbose():
result = runner.invoke(app, ["delete", "--verbose", "Camila"])
assert result.exit_code != 0
assert "Error: no such option: --verbose" 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

View file

@ -0,0 +1,25 @@
import subprocess
from commands.callback import tutorial002 as mod
from typer.testing import CliRunner
app = mod.app
runner = CliRunner()
def test_app():
result = runner.invoke(app, ["create", "Camila"])
assert result.exit_code == 0
assert "Running a command" in result.output
assert "Creating user: 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

View file

@ -0,0 +1,30 @@
import subprocess
from commands.callback import tutorial003 as mod
from typer.testing import CliRunner
app = mod.app
runner = CliRunner()
def test_app():
result = runner.invoke(app, ["create", "Camila"])
assert result.exit_code == 0
assert "Override callback, running a command" in result.output
assert "Running a command" not in result.output
assert "Creating user: Camila" in result.output
def test_for_coverage():
mod.callback()
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

View file

@ -0,0 +1,32 @@
import subprocess
from commands.callback import tutorial004 as mod
from typer.testing import CliRunner
app = mod.app
runner = CliRunner()
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Manage users CLI app." in result.output
assert "Use it with the create command." in result.output
assert "A new user with the given NAME will be created." in result.output
def test_app():
result = runner.invoke(app, ["create", "Camila"])
assert result.exit_code == 0
assert "Creating user: 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

View file

@ -0,0 +1,31 @@
import subprocess
from commands.one_or_multiple import tutorial001 as mod
from typer.testing import CliRunner
app = mod.app
runner = CliRunner()
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "create" in result.output
def test_command():
result = runner.invoke(app, ["create"])
assert result.exit_code == 0
assert "Creating user: Hiro Hamada" 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

View file

@ -0,0 +1,33 @@
import subprocess
from commands.one_or_multiple import tutorial002 as mod
from typer.testing import CliRunner
app = mod.app
runner = CliRunner()
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Creates a single user Hiro Hamada." in result.output
assert "In the next version it will create 5 users more." in result.output
assert "Commands:" in result.output
assert "create" in result.output
def test_command():
result = runner.invoke(app, ["create"])
assert result.exit_code == 0
assert "Creating user: Hiro Hamada" 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