Coverage for tests / test_tutorial / test_parameter_types / test_bool / test_tutorial002.py: 100%

46 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

7import typer 1abcdefgh

8from typer.testing import CliRunner 1abcdefgh

9 

10from ....utils import needs_py310 1abcdefgh

11 

12runner = CliRunner() 1abcdefgh

13 

14 

15@pytest.fixture( 1abcdefgh

16 name="mod", 

17 params=[ 

18 pytest.param("tutorial002_py39"), 

19 pytest.param("tutorial002_py310", marks=needs_py310), 

20 pytest.param("tutorial002_an_py39"), 

21 pytest.param("tutorial002_an_py310", marks=needs_py310), 

22 ], 

23) 

24def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefgh

25 module_name = f"docs_src.parameter_types.bool.{request.param}" 1abcdefgh

26 mod = importlib.import_module(module_name) 1abcdefgh

27 return mod 1abcdefgh

28 

29 

30def test_help(mod: ModuleType): 1abcdefgh

31 result = runner.invoke(mod.app, ["--help"]) 1abcdefgh

32 assert result.exit_code == 0 1abcdefgh

33 assert "--accept" in result.output 1abcdefgh

34 assert "--reject" in result.output 1abcdefgh

35 assert "--no-accept" not in result.output 1abcdefgh

36 

37 

38def test_help_no_rich(monkeypatch: pytest.MonkeyPatch, mod: ModuleType): 1abcdefgh

39 monkeypatch.setattr(typer.core, "HAS_RICH", False) 1abcdefgh

40 result = runner.invoke(mod.app, ["--help"]) 1abcdefgh

41 assert result.exit_code == 0 1abcdefgh

42 assert "--accept" in result.output 1abcdefgh

43 assert "--reject" in result.output 1abcdefgh

44 assert "--no-accept" not in result.output 1abcdefgh

45 

46 

47def test_main(mod: ModuleType): 1abcdefgh

48 result = runner.invoke(mod.app) 1abcdefgh

49 assert result.exit_code == 0 1abcdefgh

50 assert "I don't know what you want yet" in result.output 1abcdefgh

51 

52 

53def test_accept(mod: ModuleType): 1abcdefgh

54 result = runner.invoke(mod.app, ["--accept"]) 1abcdefgh

55 assert result.exit_code == 0 1abcdefgh

56 assert "Accepting!" in result.output 1abcdefgh

57 

58 

59def test_reject(mod: ModuleType): 1abcdefgh

60 result = runner.invoke(mod.app, ["--reject"]) 1abcdefgh

61 assert result.exit_code == 0 1abcdefgh

62 assert "Rejecting!" in result.output 1abcdefgh

63 

64 

65def test_invalid_no_accept(mod: ModuleType): 1abcdefgh

66 result = runner.invoke(mod.app, ["--no-accept"]) 1abcdefgh

67 assert result.exit_code != 0 1abcdefgh

68 assert "No such option: --no-accept" in result.output 1abcdefgh

69 

70 

71def test_script(mod: ModuleType): 1abcdefgh

72 result = subprocess.run( 1abcdefgh

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

74 capture_output=True, 

75 encoding="utf-8", 

76 ) 

77 assert "Usage" in result.stdout 1abcdefgh