Coverage for tests / test_tutorial / test_options / test_callback / test_tutorial002.py: 100%
27 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
1import importlib 1abcdefgh
2import subprocess 1abcdefgh
3import sys 1abcdefgh
4from types import ModuleType 1abcdefgh
6import pytest 1abcdefgh
7from typer.testing import CliRunner 1abcdefgh
9from ....utils import needs_py310 1abcdefgh
11runner = CliRunner() 1abcdefgh
14@pytest.fixture( 1abcdefgh
15 name="mod",
16 params=[
17 pytest.param("tutorial002_py39"),
18 pytest.param("tutorial002_py310", marks=needs_py310),
19 pytest.param("tutorial002_an_py39"),
20 pytest.param("tutorial002_an_py310", marks=needs_py310),
21 ],
22)
23def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefgh
24 module_name = f"docs_src.options.callback.{request.param}" 1abcdefgh
25 mod = importlib.import_module(module_name) 1abcdefgh
26 return mod 1abcdefgh
29def test_1(mod: ModuleType): 1abcdefgh
30 result = runner.invoke(mod.app, ["--name", "Camila"]) 1abcdefgh
31 assert result.exit_code == 0 1abcdefgh
32 assert "Validating name" in result.output 1abcdefgh
33 assert "Hello Camila" in result.output 1abcdefgh
36def test_2(mod: ModuleType): 1abcdefgh
37 result = runner.invoke(mod.app, ["--name", "rick"]) 1abcdefgh
38 assert result.exit_code != 0 1abcdefgh
39 assert "Validating name" in result.output 1abcdefgh
40 assert "Invalid value for '--name'" in result.output 1abcdefgh
41 assert "Only Camila is allowed" in result.output 1abcdefgh
44def test_script(mod: ModuleType): 1abcdefgh
45 result = subprocess.run( 1abcdefgh
46 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
47 capture_output=True,
48 encoding="utf-8",
49 )
50 assert "Usage" in result.stdout 1abcdefgh