5
5
from _pytest .pytester import Pytester
6
6
7
7
8
+ # Start of tests for classes
9
+
10
+
8
11
def run_import_class_test (pytester : Pytester , passed : int = 0 , errors : int = 0 ) -> None :
9
12
src_dir = pytester .mkdir ("src" )
10
13
tests_dir = pytester .mkdir ("tests" )
@@ -57,26 +60,37 @@ def test_collect_imports_disabled(pytester: Pytester) -> None:
57
60
58
61
run_import_class_test (pytester , passed = 1 )
59
62
63
+ # Verify that the state of hooks
64
+ reprec = pytester .inline_run ()
65
+ items_collected = reprec .getcalls ("pytest_itemcollected" )
66
+ assert len (items_collected ) == 1
67
+ for x in items_collected :
68
+ assert x .item ._getobj ().__name__ == "test_testament"
60
69
61
- def test_collect_imports_default (pytester : Pytester ) -> None :
62
- pytester .makeini ("""
63
- [pytest]
64
- testpaths = "tests"
65
- """ )
66
70
71
+ def test_collect_imports_default (pytester : Pytester ) -> None :
67
72
run_import_class_test (pytester , errors = 1 )
68
73
74
+ # TODO, hooks
75
+
69
76
70
77
def test_collect_imports_enabled (pytester : Pytester ) -> None :
71
78
pytester .makeini ("""
72
79
[pytest]
73
- testpaths = "tests"
74
80
collect_imported_tests = true
75
81
""" )
76
82
77
83
run_import_class_test (pytester , errors = 1 )
78
84
79
85
86
+ # # TODO, hooks
87
+
88
+
89
+ # End of tests for classes
90
+ #################################
91
+ # Start of tests for functions
92
+
93
+
80
94
def run_import_functions_test (
81
95
pytester : Pytester , passed : int , errors : int , failed : int
82
96
) -> None :
@@ -85,8 +99,8 @@ def run_import_functions_test(
85
99
86
100
src_file = src_dir / "foo.py"
87
101
88
- # Note that these "tests" are should _not_ be treated as tests.
89
- # They are normal functions that happens to have test_* or *_test in the name.
102
+ # Note that these "tests" should _not_ be treated as tests if `collect_imported_tests = false`
103
+ # They are normal functions in that case, that happens to have test_* or *_test in the name.
90
104
# Thus should _not_ be collected!
91
105
src_file .write_text (
92
106
textwrap .dedent ("""\
@@ -121,7 +135,6 @@ def test_important(self):
121
135
res = test_function()
122
136
if res == 5:
123
137
pass
124
-
125
138
""" ),
126
139
encoding = "utf-8" ,
127
140
)
@@ -138,31 +151,83 @@ def test_collect_function_imports_enabled(pytester: Pytester) -> None:
138
151
""" )
139
152
140
153
run_import_functions_test (pytester , passed = 2 , errors = 0 , failed = 1 )
154
+ reprec = pytester .inline_run ()
155
+ items_collected = reprec .getcalls ("pytest_itemcollected" )
156
+ # Recall that the default is `collect_imported_tests = true`.
157
+ # Which means that the normal functions are now interpreted as
158
+ # valid tests and `test_function()` will fail.
159
+ assert len (items_collected ) == 3
160
+ for x in items_collected :
161
+ assert x .item ._getobj ().__name__ in [
162
+ "test_important" ,
163
+ "test_bar" ,
164
+ "test_function" ,
165
+ ]
141
166
142
167
143
- def test_collect_function_imports_disabled (pytester : Pytester ) -> None :
168
+ def test_behaviour_without_testpaths_set_and_false (pytester : Pytester ) -> None :
169
+ # Make sure `collect_imported_tests` has no dependence on `testpaths`
144
170
pytester .makeini ("""
145
171
[pytest]
146
- # testpaths = "tests"
147
172
collect_imported_tests = false
148
173
""" )
149
174
150
175
run_import_functions_test (pytester , passed = 1 , errors = 0 , failed = 0 )
176
+ reprec = pytester .inline_run ()
177
+ items_collected = reprec .getcalls ("pytest_itemcollected" )
178
+ assert len (items_collected ) == 1
179
+ for x in items_collected :
180
+ assert x .item ._getobj ().__name__ == "test_important"
151
181
152
182
153
- def test_behaviour_without_testpaths_set_and_false (pytester : Pytester ) -> None :
183
+ def test_behaviour_without_testpaths_set_and_true (pytester : Pytester ) -> None :
184
+ # Make sure `collect_imported_tests` has no dependence on `testpaths`
154
185
pytester .makeini ("""
155
186
[pytest]
156
- collect_imported_tests = false
187
+ collect_imported_tests = true
157
188
""" )
158
189
159
- run_import_functions_test (pytester , passed = 1 , errors = 0 , failed = 0 )
190
+ run_import_functions_test (pytester , passed = 2 , errors = 0 , failed = 1 )
191
+ reprec = pytester .inline_run ()
192
+ items_collected = reprec .getcalls ("pytest_itemcollected" )
193
+ assert len (items_collected ) == 3
160
194
161
195
162
- def test_behaviour_without_testpaths_set_and_true (pytester : Pytester ) -> None :
196
+ def test_hook_behaviour_when_collect_off (pytester : Pytester ) -> None :
163
197
pytester .makeini ("""
164
198
[pytest]
165
- collect_imported_tests = true
199
+ collect_imported_tests = false
166
200
""" )
167
201
168
- run_import_functions_test (pytester , passed = 2 , errors = 0 , failed = 1 )
202
+ run_import_functions_test (pytester , passed = 1 , errors = 0 , failed = 0 )
203
+ reprec = pytester .inline_run ()
204
+
205
+ # reports = reprec.getreports("pytest_collectreport")
206
+ items_collected = reprec .getcalls ("pytest_itemcollected" )
207
+ modified = reprec .getcalls ("pytest_collection_modifyitems" )
208
+
209
+ # print("Reports: ----------------")
210
+ # print(reports)
211
+ # for r in reports:
212
+ # print(r)
213
+
214
+ # TODO this is want I want, I think....
215
+ # <CollectReport '' lenresult=1 outcome='passed'>
216
+ # <CollectReport 'tests/foo_test.py::TestDomain' lenresult=1 outcome='passed'>
217
+ # <CollectReport 'tests/foo_test.py' lenresult=1 outcome='passed'>
218
+ # <CollectReport 'tests' lenresult=1 outcome='passed'>
219
+ # <CollectReport '.' lenresult=1 outcome='passed'>
220
+
221
+ # TODO
222
+ # assert(reports.outcome == "passed")
223
+ # assert(len(reports.result) == 1)
224
+
225
+ # print("Items collected: ----------------")
226
+ # print(items_collected)
227
+ # print("Modified : ----------------")
228
+
229
+ assert len (items_collected ) == 1
230
+ for x in items_collected :
231
+ assert x .item ._getobj ().__name__ == "test_important"
232
+
233
+ assert len (modified ) == 1
0 commit comments