Coverage for tests / test_tutorial / test_subcommands / test_tutorial001.py: 100%

66 statements  

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

1import os 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4 

5import pytest 1abcdefghij

6from typer.testing import CliRunner 1abcdefghij

7 

8from docs_src.subcommands import tutorial001 1abcdefghij

9 

10runner = CliRunner() 1abcdefghij

11 

12 

13@pytest.fixture() 1abcdefghij

14def mod(monkeypatch): 1abcdefghij

15 with monkeypatch.context(): 1abcdefghij

16 monkeypatch.syspath_prepend(list(tutorial001.__path__)[0]) 1abcdefghij

17 from docs_src.subcommands.tutorial001 import main 1abcdefghij

18 

19 return main 1abcdefghij

20 

21 

22@pytest.fixture() 1abcdefghij

23def app(mod): 1abcdefghij

24 return mod.app 1abcdefghij

25 

26 

27def test_help(app): 1abcdefghij

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

29 assert result.exit_code == 0 1abcdefghij

30 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefghij

31 assert "Commands" in result.output 1abcdefghij

32 assert "items" in result.output 1abcdefghij

33 assert "users" in result.output 1abcdefghij

34 

35 

36def test_help_items(app): 1abcdefghij

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

38 assert result.exit_code == 0 1abcdefghij

39 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefghij

40 assert "Commands" in result.output 1abcdefghij

41 assert "create" in result.output 1abcdefghij

42 assert "delete" in result.output 1abcdefghij

43 assert "sell" in result.output 1abcdefghij

44 

45 

46def test_items_create(app): 1abcdefghij

47 result = runner.invoke(app, ["items", "create", "Wand"]) 1abcdefghij

48 assert result.exit_code == 0 1abcdefghij

49 assert "Creating item: Wand" in result.output 1abcdefghij

50 

51 

52def test_items_sell(app): 1abcdefghij

53 result = runner.invoke(app, ["items", "sell", "Vase"]) 1abcdefghij

54 assert result.exit_code == 0 1abcdefghij

55 assert "Selling item: Vase" in result.output 1abcdefghij

56 

57 

58def test_items_delete(app): 1abcdefghij

59 result = runner.invoke(app, ["items", "delete", "Vase"]) 1abcdefghij

60 assert result.exit_code == 0 1abcdefghij

61 assert "Deleting item: Vase" in result.output 1abcdefghij

62 

63 

64def test_help_users(app): 1abcdefghij

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

66 assert result.exit_code == 0 1abcdefghij

67 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefghij

68 assert "Commands" in result.output 1abcdefghij

69 assert "create" in result.output 1abcdefghij

70 assert "delete" in result.output 1abcdefghij

71 assert "sell" not in result.output 1abcdefghij

72 

73 

74def test_users_create(app): 1abcdefghij

75 result = runner.invoke(app, ["users", "create", "Camila"]) 1abcdefghij

76 assert result.exit_code == 0 1abcdefghij

77 assert "Creating user: Camila" in result.output 1abcdefghij

78 

79 

80def test_users_delete(app): 1abcdefghij

81 result = runner.invoke(app, ["users", "delete", "Camila"]) 1abcdefghij

82 assert result.exit_code == 0 1abcdefghij

83 assert "Deleting user: Camila" in result.output 1abcdefghij

84 

85 

86def test_scripts(mod): 1abcdefghij

87 from docs_src.subcommands.tutorial001 import items, users 1abcdefghij

88 

89 env = os.environ.copy() 1abcdefghij

90 env["PYTHONPATH"] = ":".join(list(tutorial001.__path__)) 1abcdefghij

91 

92 for module in [mod, items, users]: 1abcdefghij

93 result = subprocess.run( 1abcdefghij

94 [sys.executable, "-m", "coverage", "run", module.__file__, "--help"], 

95 capture_output=True, 

96 encoding="utf-8", 

97 env=env, 

98 ) 

99 assert "Usage" in result.stdout 1abcdefghij