Coverage for tests / test_tutorial / test_options / test_callback / test_tutorial001.py: 100%

25 statements  

« 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

5 

6import pytest 1abcdefgh

7from typer.testing import CliRunner 1abcdefgh

8 

9from ....utils import needs_py310 1abcdefgh

10 

11runner = CliRunner() 1abcdefgh

12 

13 

14@pytest.fixture( 1abcdefgh

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial001_py39"), 

18 pytest.param("tutorial001_py310", marks=needs_py310), 

19 pytest.param("tutorial001_an_py39"), 

20 pytest.param("tutorial001_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

27 

28 

29def test_1(mod: ModuleType): 1abcdefgh

30 result = runner.invoke(mod.app, ["--name", "Camila"]) 1abcdefgh

31 assert result.exit_code == 0 1abcdefgh

32 assert "Hello Camila" in result.output 1abcdefgh

33 

34 

35def test_2(mod: ModuleType): 1abcdefgh

36 result = runner.invoke(mod.app, ["--name", "rick"]) 1abcdefgh

37 assert result.exit_code != 0 1abcdefgh

38 assert "Invalid value for '--name'" in result.output 1abcdefgh

39 assert "Only Camila is allowed" in result.output 1abcdefgh

40 

41 

42def test_script(mod: ModuleType): 1abcdefgh

43 result = subprocess.run( 1abcdefgh

44 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], 

45 capture_output=True, 

46 encoding="utf-8", 

47 ) 

48 assert "Usage" in result.stdout 1abcdefgh