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

120 statements  

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

1import os 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4 

5import pytest 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8from docs_src.subcommands import tutorial003_py39 1abcdefgh

9from docs_src.subcommands.tutorial003_py39 import items, users 1abcdefgh

10 

11runner = CliRunner() 1abcdefgh

12 

13 

14@pytest.fixture() 1abcdefgh

15def mod(monkeypatch): 1abcdefgh

16 with monkeypatch.context() as m: 1abcdefgh

17 m.syspath_prepend(list(tutorial003_py39.__path__)[0]) 1abcdefgh

18 from docs_src.subcommands.tutorial003_py39 import main 1abcdefgh

19 

20 return main 1abcdefgh

21 

22 

23@pytest.fixture() 1abcdefgh

24def app(mod): 1abcdefgh

25 return mod.app 1abcdefgh

26 

27 

28def test_help(app): 1abcdefgh

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

30 assert result.exit_code == 0 1abcdefgh

31 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

32 assert "Commands" in result.output 1abcdefgh

33 assert "items" in result.output 1abcdefgh

34 assert "users" in result.output 1abcdefgh

35 assert "lands" in result.output 1abcdefgh

36 

37 

38def test_help_items(app): 1abcdefgh

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

40 assert result.exit_code == 0 1abcdefgh

41 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

42 assert "Commands" in result.output 1abcdefgh

43 assert "create" in result.output 1abcdefgh

44 assert "delete" in result.output 1abcdefgh

45 assert "sell" in result.output 1abcdefgh

46 

47 

48def test_items_create(app): 1abcdefgh

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

50 assert result.exit_code == 0 1abcdefgh

51 assert "Creating item: Wand" in result.output 1abcdefgh

52 # For coverage, because the monkeypatch above sometimes confuses coverage 

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

54 assert result.exit_code == 0 1abcdefgh

55 assert "Creating item: Wand" in result.output 1abcdefgh

56 

57 

58def test_items_sell(app): 1abcdefgh

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

60 assert result.exit_code == 0 1abcdefgh

61 assert "Selling item: Vase" in result.output 1abcdefgh

62 # For coverage, because the monkeypatch above sometimes confuses coverage 

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

64 assert result.exit_code == 0 1abcdefgh

65 assert "Selling item: Vase" in result.output 1abcdefgh

66 

67 

68def test_items_delete(app): 1abcdefgh

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

70 assert result.exit_code == 0 1abcdefgh

71 assert "Deleting item: Vase" in result.output 1abcdefgh

72 # For coverage, because the monkeypatch above sometimes confuses coverage 

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

74 assert result.exit_code == 0 1abcdefgh

75 assert "Deleting item: Vase" in result.output 1abcdefgh

76 

77 

78def test_help_users(app): 1abcdefgh

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

80 assert result.exit_code == 0 1abcdefgh

81 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

82 assert "Commands" in result.output 1abcdefgh

83 assert "create" in result.output 1abcdefgh

84 assert "delete" in result.output 1abcdefgh

85 assert "sell" not in result.output 1abcdefgh

86 

87 

88def test_users_create(app): 1abcdefgh

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

90 assert result.exit_code == 0 1abcdefgh

91 assert "Creating user: Camila" in result.output 1abcdefgh

92 # For coverage, because the monkeypatch above sometimes confuses coverage 

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

94 assert result.exit_code == 0 1abcdefgh

95 assert "Creating user: Camila" in result.output 1abcdefgh

96 

97 

98def test_users_delete(app): 1abcdefgh

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

100 assert result.exit_code == 0 1abcdefgh

101 assert "Deleting user: Camila" in result.output 1abcdefgh

102 # For coverage, because the monkeypatch above sometimes confuses coverage 

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

104 assert result.exit_code == 0 1abcdefgh

105 assert "Deleting user: Camila" in result.output 1abcdefgh

106 

107 

108def test_help_lands(app): 1abcdefgh

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

110 assert result.exit_code == 0 1abcdefgh

111 assert "lands [OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

112 assert "Commands" in result.output 1abcdefgh

113 assert "reigns" in result.output 1abcdefgh

114 assert "towns" in result.output 1abcdefgh

115 

116 

117def test_help_lands_reigns(app): 1abcdefgh

118 result = runner.invoke(app, ["lands", "reigns", "--help"]) 1abcdefgh

119 assert result.exit_code == 0 1abcdefgh

120 assert "lands reigns [OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

121 assert "Commands" in result.output 1abcdefgh

122 assert "conquer" in result.output 1abcdefgh

123 assert "destroy" in result.output 1abcdefgh

124 

125 

126def test_lands_reigns_conquer(app): 1abcdefgh

127 result = runner.invoke(app, ["lands", "reigns", "conquer", "Gondor"]) 1abcdefgh

128 assert result.exit_code == 0 1abcdefgh

129 assert "Conquering reign: Gondor" in result.output 1abcdefgh

130 

131 

132def test_lands_reigns_destroy(app): 1abcdefgh

133 result = runner.invoke(app, ["lands", "reigns", "destroy", "Mordor"]) 1abcdefgh

134 assert result.exit_code == 0 1abcdefgh

135 assert "Destroying reign: Mordor" in result.output 1abcdefgh

136 

137 

138def test_help_lands_towns(app): 1abcdefgh

139 result = runner.invoke(app, ["lands", "towns", "--help"]) 1abcdefgh

140 assert result.exit_code == 0 1abcdefgh

141 assert "lands towns [OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

142 assert "Commands" in result.output 1abcdefgh

143 assert "burn" in result.output 1abcdefgh

144 assert "found" in result.output 1abcdefgh

145 

146 

147def test_lands_towns_found(app): 1abcdefgh

148 result = runner.invoke(app, ["lands", "towns", "found", "Cartagena"]) 1abcdefgh

149 assert result.exit_code == 0 1abcdefgh

150 assert "Founding town: Cartagena" in result.output 1abcdefgh

151 

152 

153def test_lands_towns_burn(app): 1abcdefgh

154 result = runner.invoke(app, ["lands", "towns", "burn", "New Asgard"]) 1abcdefgh

155 assert result.exit_code == 0 1abcdefgh

156 assert "Burning town: New Asgard" in result.output 1abcdefgh

157 

158 

159def test_scripts(mod): 1abcdefgh

160 from docs_src.subcommands.tutorial003_py39 import items, lands, reigns, towns, users 1abcdefgh

161 

162 env = os.environ.copy() 1abcdefgh

163 env["PYTHONPATH"] = ":".join(list(tutorial003_py39.__path__)) 1abcdefgh

164 

165 for module in [mod, items, lands, reigns, towns, users]: 1abcdefgh

166 result = subprocess.run( 1abcdefgh

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

168 capture_output=True, 

169 encoding="utf-8", 

170 env=env, 

171 ) 

172 assert "Usage" in result.stdout 1abcdefgh