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

60 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1import importlib 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4from types import ModuleType 1abcdefghij

5 

6import pytest 1abcdefghij

7import typer 1abcdefghij

8import typer.core 1abcdefghij

9from typer.testing import CliRunner 1abcdefghij

10 

11runner = CliRunner() 1abcdefghij

12 

13 

14@pytest.fixture( 1abcdefghij

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial001_non_an"), 

18 pytest.param("tutorial001"), 

19 ], 

20) 

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

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

23 mod = importlib.import_module(module_name) 1abcdefghij

24 return mod 1abcdefghij

25 

26 

27def test_help(mod: ModuleType): 1abcdefghij

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

29 assert result.exit_code == 0 1abcdefghij

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

31 assert "int range" in result.output 1abcdefghij

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

33 assert "float range" in result.output 1abcdefghij

34 

35 

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

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

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

39 assert result.exit_code == 0 1abcdefghij

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

41 assert "int range" in result.output 1abcdefghij

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

43 assert "float range" in result.output 1abcdefghij

44 

45 

46def test_params(mod: ModuleType): 1abcdefghij

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

48 assert result.exit_code == 0 1abcdefghij

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

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

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

52 

53 

54def test_invalid_id(mod: ModuleType): 1abcdefghij

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

56 assert result.exit_code != 0 1abcdefghij

57 assert "Invalid value for 'ID'" in result.output 1abcdefghij

58 assert "should be less than or equal to 1000" in result.output 1abcdefghij

59 

60 

61def test_invalid_age(mod: ModuleType): 1abcdefghij

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

63 assert result.exit_code != 0 1abcdefghij

64 assert "Invalid value for '--age'" in result.output 1abcdefghij

65 assert "should be greater than or equal to 18" in result.output 1abcdefghij

66 

67 

68def test_invalid_score(monkeypatch: pytest.MonkeyPatch, mod: ModuleType): 1abcdefghij

69 monkeypatch.setattr(typer.core, "HAS_RICH", False) 1abcdefghij

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

71 assert result.exit_code != 0 1abcdefghij

72 assert "Invalid value for '--score'" in result.output 1abcdefghij

73 assert "should be less than or equal to 100" in result.output 1abcdefghij

74 

75 

76def test_negative_score(mod: ModuleType): 1abcdefghij

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

78 assert result.exit_code == 0 1abcdefghij

79 assert "ID is 5" in result.output 1abcdefghij

80 assert "--age is 20" in result.output 1abcdefghij

81 assert "--score is -5.0" in result.output 1abcdefghij

82 

83 

84def test_script(mod: ModuleType): 1abcdefghij

85 result = subprocess.run( 1abcdefghij

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

87 capture_output=True, 

88 encoding="utf-8", 

89 ) 

90 assert "Usage" in result.stdout 1abcdefghij