Add tests for subcommands examples

This commit is contained in:
Sebastián Ramírez 2020-01-05 20:49:29 +01:00
parent daeb3b7656
commit 44319119e9
16 changed files with 607 additions and 42 deletions

View file

@ -0,0 +1,26 @@
import subprocess
from typer.testing import CliRunner
from subcommands.callback_override import tutorial001 as mod
runner = CliRunner()
app = mod.app
def test_cli():
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Running a users 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,26 @@
import subprocess
from typer.testing import CliRunner
from subcommands.callback_override import tutorial002 as mod
runner = CliRunner()
app = mod.app
def test_cli():
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Running a users 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,31 @@
import subprocess
from typer.testing import CliRunner
from subcommands.callback_override import tutorial003 as mod
runner = CliRunner()
app = mod.app
def test_cli():
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Running a users command" not in result.output
assert "Callback override, running users command" in result.output
assert "Creating user: Camila" in result.output
def test_for_coverage():
mod.default_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,33 @@
import subprocess
from typer.testing import CliRunner
from subcommands.callback_override import tutorial004 as mod
runner = CliRunner()
app = mod.app
def test_cli():
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Running a users command" not in result.output
assert "Callback override, running users command" not in result.output
assert "I have the high land! Running users command" in result.output
assert "Creating user: Camila" in result.output
def test_for_coverage():
mod.default_callback()
mod.user_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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial001 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "users" in result.output
assert "Manage users in the app." in result.output
def test_command_help():
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "Manage users in the app." in result.output
def test_command():
result = runner.invoke(app, ["users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial002 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "users" in result.output
assert "Manage users in the app." in result.output
def test_command_help():
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "Manage users in the app." in result.output
def test_command():
result = runner.invoke(app, ["users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial003 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "users" in result.output
assert "Manage users in the app." in result.output
def test_command_help():
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "Manage users in the app." in result.output
def test_command():
result = runner.invoke(app, ["users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial004 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "users" in result.output
assert "Manage users in the app." in result.output
def test_command_help():
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "Manage users in the app." in result.output
def test_command():
result = runner.invoke(app, ["users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial005 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "new-users" in result.output
assert "I have the highland! Create some users." in result.output
def test_command_help():
result = runner.invoke(app, ["new-users", "--help"])
assert result.exit_code == 0
assert "I have the highland! Create some users." in result.output
def test_command():
result = runner.invoke(app, ["new-users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial006 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "exp-users" in result.output
assert "Explicit help." in result.output
def test_command_help():
result = runner.invoke(app, ["exp-users", "--help"])
assert result.exit_code == 0
assert "Explicit help." in result.output
def test_command():
result = runner.invoke(app, ["exp-users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial007 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "call-users" in result.output
assert "Help from callback for users." in result.output
def test_command_help():
result = runner.invoke(app, ["call-users", "--help"])
assert result.exit_code == 0
assert "Help from callback for users." in result.output
def test_command():
result = runner.invoke(app, ["call-users", "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,39 @@
import subprocess
from typer.testing import CliRunner
from subcommands.name_help import tutorial008 as mod
runner = CliRunner()
app = mod.app
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
assert "cake-sith-users" in result.output
assert "Unlimited powder! Eh, users." in result.output
def test_command_help():
result = runner.invoke(app, ["cake-sith-users", "--help"])
assert result.exit_code == 0
assert "Unlimited powder! Eh, users." in result.output
def test_command():
result = runner.invoke(app, ["cake-sith-users", "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

@ -1,20 +1,28 @@
import subprocess
import sys
import pytest
from typer.testing import CliRunner
from subcommands import tutorial001
from subcommands.tutorial001 import items, users
sys.path.extend(tutorial001.__path__) # isort:skip
from subcommands.tutorial001 import main as mod # isort:skip
app = mod.app
runner = CliRunner()
def test_help():
@pytest.fixture()
def mod(monkeypatch):
with monkeypatch.context() as m:
monkeypatch.syspath_prepend(list(tutorial001.__path__)[0])
from subcommands.tutorial001 import main
return main
@pytest.fixture()
def app(mod):
return mod.app
def test_help(app):
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
@ -23,7 +31,7 @@ def test_help():
assert "users" in result.output
def test_help_items():
def test_help_items(app):
result = runner.invoke(app, ["items", "--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
@ -33,25 +41,25 @@ def test_help_items():
assert "sell" in result.output
def test_items_create():
def test_items_create(app):
result = runner.invoke(app, ["items", "create", "Wand"])
assert result.exit_code == 0
assert "Creating item: Wand" in result.output
def test_items_sell():
def test_items_sell(app):
result = runner.invoke(app, ["items", "sell", "Vase"])
assert result.exit_code == 0
assert "Selling item: Vase" in result.output
def test_items_delete():
def test_items_delete(app):
result = runner.invoke(app, ["items", "delete", "Vase"])
assert result.exit_code == 0
assert "Deleting item: Vase" in result.output
def test_help_users():
def test_help_users(app):
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
@ -61,43 +69,26 @@ def test_help_users():
assert "sell" not in result.output
def test_users_create():
def test_users_create(app):
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Creating user: Camila" in result.output
def test_users_delete():
def test_users_delete(app):
result = runner.invoke(app, ["users", "delete", "Camila"])
assert result.exit_code == 0
assert "Deleting 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
def test_scripts(mod):
from subcommands.tutorial001 import items, users
def test_script_items():
result = subprocess.run(
["coverage", "run", items.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout
def test_script_users():
result = subprocess.run(
["coverage", "run", users.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout
for module in [mod, items, users]:
result = subprocess.run(
["coverage", "run", module.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout

View file

@ -0,0 +1,146 @@
import subprocess
import pytest
from typer.testing import CliRunner
from subcommands import tutorial003
runner = CliRunner()
@pytest.fixture()
def mod(monkeypatch):
with monkeypatch.context() as m:
m.syspath_prepend(list(tutorial003.__path__)[0])
from subcommands.tutorial003 import main
return main
@pytest.fixture()
def app(mod):
return mod.app
def test_help(app):
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands:" in result.output
assert "items" in result.output
assert "users" in result.output
assert "lands" in result.output
def test_help_items(app):
result = runner.invoke(app, ["items", "--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands:" in result.output
assert "create" in result.output
assert "delete" in result.output
assert "sell" in result.output
def test_items_create(app):
result = runner.invoke(app, ["items", "create", "Wand"])
assert result.exit_code == 0
assert "Creating item: Wand" in result.output
def test_items_sell(app):
result = runner.invoke(app, ["items", "sell", "Vase"])
assert result.exit_code == 0
assert "Selling item: Vase" in result.output
def test_items_delete(app):
result = runner.invoke(app, ["items", "delete", "Vase"])
assert result.exit_code == 0
assert "Deleting item: Vase" in result.output
def test_help_users(app):
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands:" in result.output
assert "create" in result.output
assert "delete" in result.output
assert "sell" not in result.output
def test_users_create(app):
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Creating user: Camila" in result.output
def test_users_delete(app):
result = runner.invoke(app, ["users", "delete", "Camila"])
assert result.exit_code == 0
assert "Deleting user: Camila" in result.output
def test_help_lands(app):
result = runner.invoke(app, ["lands", "--help"])
assert result.exit_code == 0
assert "lands [OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands:" in result.output
assert "reigns" in result.output
assert "towns" in result.output
def test_help_lands_reigns(app):
result = runner.invoke(app, ["lands", "reigns", "--help"])
assert result.exit_code == 0
assert "lands reigns [OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands:" in result.output
assert "conquer" in result.output
assert "destroy" in result.output
def test_lands_reigns_conquer(app):
result = runner.invoke(app, ["lands", "reigns", "conquer", "Gondor"])
assert result.exit_code == 0
assert "Conquering reign: Gondor" in result.output
def test_lands_reigns_destroy(app):
result = runner.invoke(app, ["lands", "reigns", "destroy", "Mordor"])
assert result.exit_code == 0
assert "Destroying reign: Mordor" in result.output
def test_help_lands_towns(app):
result = runner.invoke(app, ["lands", "towns", "--help"])
assert result.exit_code == 0
assert "lands towns [OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands:" in result.output
assert "burn" in result.output
assert "found" in result.output
def test_lands_towns_found(app):
result = runner.invoke(app, ["lands", "towns", "found", "Cartagena"])
assert result.exit_code == 0
assert "Founding town: Cartagena" in result.output
def test_lands_towns_burn(app):
result = runner.invoke(app, ["lands", "towns", "burn", "New Asgard"])
assert result.exit_code == 0
assert "Burning town: New Asgard" in result.output
def test_scripts(mod):
from subcommands.tutorial003 import items, lands, reigns, towns, users
for module in [mod, items, lands, reigns, towns, users]:
result = subprocess.run(
["coverage", "run", module.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout