Coverage for tests / test_tutorial / test_parameter_types / test_number / test_tutorial001.py: 100%

59 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

8import typer.core 1abcdefgh

9from typer.testing import CliRunner 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_an_py39"), 

19 ], 

20) 

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

22 module_name = f"docs_src.parameter_types.number.{request.param}" 1abcdefgh

23 mod = importlib.import_module(module_name) 1abcdefgh

24 return mod 1abcdefgh

25 

26 

27def test_help(mod: ModuleType): 1abcdefgh

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

29 assert result.exit_code == 0 1abcdefgh

30 assert "--age" in result.output 1abcdefgh

31 assert "INTEGER RANGE" in result.output 1abcdefgh

32 assert "--score" in result.output 1abcdefgh

33 assert "FLOAT RANGE" in result.output 1abcdefgh

34 

35 

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

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

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

39 assert result.exit_code == 0 1abcdefgh

40 assert "--age" in result.output 1abcdefgh

41 assert "INTEGER RANGE" in result.output 1abcdefgh

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

43 assert "FLOAT RANGE" in result.output 1abcdefgh

44 

45 

46def test_params(mod: ModuleType): 1abcdefgh

47 result = runner.invoke(mod.app, ["5", "--age", "20", "--score", "90"]) 1abcdefgh

48 assert result.exit_code == 0 1abcdefgh

49 assert "ID is 5" in result.output 1abcdefgh

50 assert "--age is 20" in result.output 1abcdefgh

51 assert "--score is 90.0" in result.output 1abcdefgh

52 

53 

54def test_invalid_id(mod: ModuleType): 1abcdefgh

55 result = runner.invoke(mod.app, ["1002"]) 1abcdefgh

56 assert result.exit_code != 0 1abcdefgh

57 assert ( 1abcdefgh

58 "Invalid value for 'ID': 1002 is not in the range 0<=x<=1000." in result.output 

59 ) 

60 

61 

62def test_invalid_age(mod: ModuleType): 1abcdefgh

63 result = runner.invoke(mod.app, ["5", "--age", "15"]) 1abcdefgh

64 assert result.exit_code != 0 1abcdefgh

65 assert "Invalid value for '--age'" in result.output 1abcdefgh

66 assert "15 is not in the range x>=18" in result.output 1abcdefgh

67 

68 

69def test_invalid_score(monkeypatch: pytest.MonkeyPatch, mod: ModuleType): 1abcdefgh

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

71 result = runner.invoke(mod.app, ["5", "--age", "20", "--score", "100.5"]) 1abcdefgh

72 assert result.exit_code != 0 1abcdefgh

73 assert "Invalid value for '--score'" in result.output 1abcdefgh

74 assert "100.5 is not in the range x<=100." in result.output 1abcdefgh

75 

76 

77def test_negative_score(mod: ModuleType): 1abcdefgh

78 result = runner.invoke(mod.app, ["5", "--age", "20", "--score", "-5"]) 1abcdefgh

79 assert result.exit_code == 0 1abcdefgh

80 assert "ID is 5" in result.output 1abcdefgh

81 assert "--age is 20" in result.output 1abcdefgh

82 assert "--score is -5.0" in result.output 1abcdefgh

83 

84 

85def test_script(mod: ModuleType): 1abcdefgh

86 result = subprocess.run( 1abcdefgh

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

88 capture_output=True, 

89 encoding="utf-8", 

90 ) 

91 assert "Usage" in result.stdout 1abcdefgh