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

120 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 tutorial003 1abcdefghij

9from docs_src.subcommands.tutorial003 import items, users 1abcdefghij

10 

11runner = CliRunner() 1abcdefghij

12 

13 

14@pytest.fixture() 1abcdefghij

15def mod(monkeypatch): 1abcdefghij

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

17 m.syspath_prepend(list(tutorial003.__path__)[0]) 1abcdefghij

18 from docs_src.subcommands.tutorial003 import main 1abcdefghij

19 

20 return main 1abcdefghij

21 

22 

23@pytest.fixture() 1abcdefghij

24def app(mod): 1abcdefghij

25 return mod.app 1abcdefghij

26 

27 

28def test_help(app): 1abcdefghij

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

30 assert result.exit_code == 0 1abcdefghij

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

32 assert "Commands" in result.output 1abcdefghij

33 assert "items" in result.output 1abcdefghij

34 assert "users" in result.output 1abcdefghij

35 assert "lands" in result.output 1abcdefghij

36 

37 

38def test_help_items(app): 1abcdefghij

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

40 assert result.exit_code == 0 1abcdefghij

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

42 assert "Commands" in result.output 1abcdefghij

43 assert "create" in result.output 1abcdefghij

44 assert "delete" in result.output 1abcdefghij

45 assert "sell" in result.output 1abcdefghij

46 

47 

48def test_items_create(app): 1abcdefghij

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

50 assert result.exit_code == 0 1abcdefghij

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

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

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

54 assert result.exit_code == 0 1abcdefghij

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

56 

57 

58def test_items_sell(app): 1abcdefghij

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

60 assert result.exit_code == 0 1abcdefghij

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

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

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

64 assert result.exit_code == 0 1abcdefghij

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

66 

67 

68def test_items_delete(app): 1abcdefghij

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

70 assert result.exit_code == 0 1abcdefghij

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

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

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

74 assert result.exit_code == 0 1abcdefghij

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

76 

77 

78def test_help_users(app): 1abcdefghij

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

80 assert result.exit_code == 0 1abcdefghij

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

82 assert "Commands" in result.output 1abcdefghij

83 assert "create" in result.output 1abcdefghij

84 assert "delete" in result.output 1abcdefghij

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

86 

87 

88def test_users_create(app): 1abcdefghij

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

90 assert result.exit_code == 0 1abcdefghij

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

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

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

94 assert result.exit_code == 0 1abcdefghij

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

96 

97 

98def test_users_delete(app): 1abcdefghij

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

100 assert result.exit_code == 0 1abcdefghij

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

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

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

104 assert result.exit_code == 0 1abcdefghij

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

106 

107 

108def test_help_lands(app): 1abcdefghij

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

110 assert result.exit_code == 0 1abcdefghij

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

112 assert "Commands" in result.output 1abcdefghij

113 assert "reigns" in result.output 1abcdefghij

114 assert "towns" in result.output 1abcdefghij

115 

116 

117def test_help_lands_reigns(app): 1abcdefghij

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

119 assert result.exit_code == 0 1abcdefghij

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

121 assert "Commands" in result.output 1abcdefghij

122 assert "conquer" in result.output 1abcdefghij

123 assert "destroy" in result.output 1abcdefghij

124 

125 

126def test_lands_reigns_conquer(app): 1abcdefghij

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

128 assert result.exit_code == 0 1abcdefghij

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

130 

131 

132def test_lands_reigns_destroy(app): 1abcdefghij

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

134 assert result.exit_code == 0 1abcdefghij

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

136 

137 

138def test_help_lands_towns(app): 1abcdefghij

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

140 assert result.exit_code == 0 1abcdefghij

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

142 assert "Commands" in result.output 1abcdefghij

143 assert "burn" in result.output 1abcdefghij

144 assert "found" in result.output 1abcdefghij

145 

146 

147def test_lands_towns_found(app): 1abcdefghij

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

149 assert result.exit_code == 0 1abcdefghij

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

151 

152 

153def test_lands_towns_burn(app): 1abcdefghij

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

155 assert result.exit_code == 0 1abcdefghij

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

157 

158 

159def test_scripts(mod): 1abcdefghij

160 from docs_src.subcommands.tutorial003 import ( 1abcdefghij

161 items, 

162 lands, 

163 reigns, 

164 towns, 

165 users, 

166 ) 

167 

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

169 env["PYTHONPATH"] = ":".join(list(tutorial003.__path__)) 1abcdefghij

170 

171 for module in [mod, items, lands, reigns, towns, users]: 1abcdefghij

172 result = subprocess.run( 1abcdefghij

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

174 capture_output=True, 

175 encoding="utf-8", 

176 env=env, 

177 ) 

178 assert "Usage" in result.stdout 1abcdefghij