Skip to content

Commit f1821ea

Browse files
committed
WIP - report collection needs triage
1 parent 191456e commit f1821ea

File tree

1 file changed

+148
-130
lines changed

1 file changed

+148
-130
lines changed

testing/test_collect_imports.py

Lines changed: 148 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -13,78 +13,82 @@
1313

1414
# Start of tests for classes
1515

16-
# def run_import_class_test(pytester: Pytester, passed: int = 0, errors: int = 0) -> None:
17-
# src_dir = pytester.mkdir("src")
18-
# tests_dir = pytester.mkdir("tests")
19-
# src_file = src_dir / "foo.py"
20-
21-
# src_file.write_text(
22-
# textwrap.dedent("""\
23-
# class Testament(object):
24-
# def __init__(self):
25-
# super().__init__()
26-
# self.collections = ["stamp", "coin"]
27-
28-
# def personal_property(self):
29-
# return [f"my {x} collection" for x in self.collections]
30-
# """),
31-
# encoding="utf-8",
32-
# )
33-
34-
# test_file = tests_dir / "foo_test.py"
35-
# test_file.write_text(
36-
# textwrap.dedent("""\
37-
# import sys
38-
# import os
39-
40-
# current_file = os.path.abspath(__file__)
41-
# current_dir = os.path.dirname(current_file)
42-
# parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
43-
# sys.path.append(parent_dir)
44-
45-
# from src.foo import Testament
46-
47-
# class TestDomain:
48-
# def test_testament(self):
49-
# testament = Testament()
50-
# assert testament.personal_property()
51-
# """),
52-
# encoding="utf-8",
53-
# )
54-
55-
# result = pytester.runpytest()
56-
# result.assert_outcomes(passed=passed, errors=errors)
57-
58-
# def test_collect_imports_disabled(pytester: Pytester) -> None:
59-
# pytester.makeini("""
60-
# [pytest]
61-
# testpaths = "tests"
62-
# collect_imported_tests = false
63-
# """)
64-
65-
# run_import_class_test(pytester, passed=1)
66-
67-
# # Verify that the state of hooks
68-
# reprec = pytester.inline_run()
69-
# items_collected = reprec.getcalls("pytest_itemcollected")
70-
# assert len(items_collected) == 1
71-
# for x in items_collected:
72-
# assert x.item._getobj().__name__ == "test_testament"
73-
74-
# def test_collect_imports_default(pytester: Pytester) -> None:
75-
# run_import_class_test(pytester, errors=1)
76-
77-
# # TODO, hooks
78-
79-
80-
# def test_collect_imports_enabled(pytester: Pytester) -> None:
81-
# pytester.makeini("""
82-
# [pytest]
83-
# collect_imported_tests = true
84-
# """)
85-
86-
# run_import_class_test(pytester, errors=1)
87-
# # TODO, hooks
16+
17+
def run_import_class_test(
18+
pytester: Pytester, passed: int = 0, errors: int = 0
19+
) -> RunResult:
20+
src_dir = pytester.mkdir("src")
21+
tests_dir = pytester.mkdir("tests")
22+
src_file = src_dir / "foo.py"
23+
24+
src_file.write_text(
25+
textwrap.dedent("""\
26+
class Testament(object):
27+
def __init__(self):
28+
super().__init__()
29+
self.collections = ["stamp", "coin"]
30+
31+
def personal_property(self):
32+
return [f"my {x} collection" for x in self.collections]
33+
"""),
34+
encoding="utf-8",
35+
)
36+
37+
test_file = tests_dir / "foo_test.py"
38+
test_file.write_text(
39+
textwrap.dedent("""\
40+
import sys
41+
import os
42+
43+
current_file = os.path.abspath(__file__)
44+
current_dir = os.path.dirname(current_file)
45+
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
46+
sys.path.append(parent_dir)
47+
48+
from src.foo import Testament
49+
50+
class TestDomain:
51+
def test_testament(self):
52+
testament = Testament()
53+
assert testament.personal_property()
54+
"""),
55+
encoding="utf-8",
56+
)
57+
58+
result = pytester.runpytest()
59+
result.assert_outcomes(passed=passed, errors=errors)
60+
return result
61+
62+
63+
def test_collect_imports_disabled(pytester: Pytester) -> None:
64+
pytester.makeini("""
65+
[pytest]
66+
testpaths = "tests"
67+
collect_imported_tests = false
68+
""")
69+
70+
run_import_class_test(pytester, passed=1)
71+
72+
# Verify that the state of hooks
73+
reprec = pytester.inline_run()
74+
items_collected = reprec.getcalls("pytest_itemcollected")
75+
assert len(items_collected) == 1
76+
for x in items_collected:
77+
assert x.item._getobj().__name__ == "test_testament"
78+
79+
80+
def test_collect_imports_default(pytester: Pytester) -> None:
81+
run_import_class_test(pytester, errors=1)
82+
# TODO
83+
84+
85+
def test_collect_imports_enabled(pytester: Pytester) -> None:
86+
pytester.makeini("""
87+
[pytest]
88+
collect_imported_tests = true
89+
""")
90+
91+
run_import_class_test(pytester, errors=1)
8892

8993

9094
# End of tests for classes
@@ -144,54 +148,54 @@ def test_important(self):
144148
return result
145149

146150

147-
# def test_collect_function_imports_enabled(pytester: Pytester) -> None:
148-
# pytester.makeini("""
149-
# [pytest]
150-
# testpaths = "tests"
151-
# collect_imported_tests = true
152-
# """)
153-
154-
# run_import_functions_test(pytester, passed=2, errors=0, failed=1)
155-
# reprec = pytester.inline_run()
156-
# items_collected = reprec.getcalls("pytest_itemcollected")
157-
# # Recall that the default is `collect_imported_tests = true`.
158-
# # Which means that the normal functions are now interpreted as
159-
# # valid tests and `test_function()` will fail.
160-
# assert len(items_collected) == 3
161-
# for x in items_collected:
162-
# assert x.item._getobj().__name__ in [
163-
# "test_important",
164-
# "test_bar",
165-
# "test_function",
166-
# ]
167-
168-
169-
# def test_behaviour_without_testpaths_set_and_false(pytester: Pytester) -> None:
170-
# # Make sure `collect_imported_tests` has no dependence on `testpaths`
171-
# pytester.makeini("""
172-
# [pytest]
173-
# collect_imported_tests = false
174-
# """)
175-
176-
# run_import_functions_test(pytester, passed=1, errors=0, failed=0)
177-
# reprec = pytester.inline_run()
178-
# items_collected = reprec.getcalls("pytest_itemcollected")
179-
# assert len(items_collected) == 1
180-
# for x in items_collected:
181-
# assert x.item._getobj().__name__ == "test_important"
182-
183-
184-
# def test_behaviour_without_testpaths_set_and_true(pytester: Pytester) -> None:
185-
# # Make sure `collect_imported_tests` has no dependence on `testpaths`
186-
# pytester.makeini("""
187-
# [pytest]
188-
# collect_imported_tests = true
189-
# """)
190-
191-
# run_import_functions_test(pytester, passed=2, errors=0, failed=1)
192-
# reprec = pytester.inline_run()
193-
# items_collected = reprec.getcalls("pytest_itemcollected")
194-
# assert len(items_collected) == 3
151+
def test_collect_function_imports_enabled(pytester: Pytester) -> None:
152+
pytester.makeini("""
153+
[pytest]
154+
testpaths = "tests"
155+
collect_imported_tests = true
156+
""")
157+
158+
run_import_functions_test(pytester, passed=2, errors=0, failed=1)
159+
reprec = pytester.inline_run()
160+
items_collected = reprec.getcalls("pytest_itemcollected")
161+
# Recall that the default is `collect_imported_tests = true`.
162+
# Which means that the normal functions are now interpreted as
163+
# valid tests and `test_function()` will fail.
164+
assert len(items_collected) == 3
165+
for x in items_collected:
166+
assert x.item._getobj().__name__ in [
167+
"test_important",
168+
"test_bar",
169+
"test_function",
170+
]
171+
172+
173+
def test_behaviour_without_testpaths_set_and_false(pytester: Pytester) -> None:
174+
# Make sure `collect_imported_tests` has no dependence on `testpaths`
175+
pytester.makeini("""
176+
[pytest]
177+
collect_imported_tests = false
178+
""")
179+
180+
run_import_functions_test(pytester, passed=1, errors=0, failed=0)
181+
reprec = pytester.inline_run()
182+
items_collected = reprec.getcalls("pytest_itemcollected")
183+
assert len(items_collected) == 1
184+
for x in items_collected:
185+
assert x.item._getobj().__name__ == "test_important"
186+
187+
188+
def test_behaviour_without_testpaths_set_and_true(pytester: Pytester) -> None:
189+
# Make sure `collect_imported_tests` has no dependence on `testpaths`
190+
pytester.makeini("""
191+
[pytest]
192+
collect_imported_tests = true
193+
""")
194+
195+
run_import_functions_test(pytester, passed=2, errors=0, failed=1)
196+
reprec = pytester.inline_run()
197+
items_collected = reprec.getcalls("pytest_itemcollected")
198+
assert len(items_collected) == 3
195199

196200

197201
class TestHookBehaviour:
@@ -255,11 +259,12 @@ def _test_hook_behaviour_when_collect_off(self, pytester: Pytester) -> None:
255259
"reports": reports,
256260
}
257261

258-
# Now check that the two tests above did indeed result in the same outcome.
259262
def _test_hook_behaviour(self) -> None:
260263
print("ABCD", self.collect_outcomes)
261264
default = self.collect_outcomes["default"]
262265
collect_off = self.collect_outcomes["collect_off"]
266+
267+
# Check that the two tests above did indeed result in the same outcome.
263268
assert default["result"] == collect_off["result"]
264269

265270
assert len(default["modified"]) == len(collect_off["modified"]) == 1
@@ -282,13 +287,14 @@ def _test_hook_behaviour(self) -> None:
282287
len(default["items_collected"]) == len(collect_off["items_collected"]) == 1
283288
)
284289

290+
# Check if the same tests got collected
285291
def_items_record: RecordedHookCall = default["items_collected"][0]
286292
off_items_record: RecordedHookCall = collect_off["items_collected"][0]
287293
def_items = def_items_record.__dict__["item"]
288294
off_items = off_items_record.__dict__["item"]
289295
assert def_items.name == off_items.name
290296

291-
# TODO: fix diff:
297+
# TODO: fix diff: (This will get cleaned up)
292298
# [
293299
# <CollectReport '' lenresult=1 outcome='passed'>,
294300
# - <CollectReport 'src' lenresult=0 outcome='passed'>,
@@ -301,12 +307,24 @@ def _test_hook_behaviour(self) -> None:
301307
# ? ^
302308
# ]
303309

304-
# assert len(default['reports']) == len(collect_off['reports'])
305-
# for i in range(len(default['reports'])):
306-
# print("def",default['reports'][i].__dict__)
307-
# print("off",collect_off['reports'][i].__dict__)
310+
for x in default["reports"]:
311+
print("def", x.__dict__)
312+
313+
for x in collect_off["reports"]:
314+
print("off", x.__dict__)
315+
316+
# The two above loops prints:
317+
318+
# def {'nodeid': '', 'outcome': 'passed', 'longrepr': None, 'result': [<Dir test_hook_behaviour0>], 'sections': []} # noqa: E501
319+
# def {'nodeid': 'tests/foo_test.py::TestDomain', 'outcome': 'passed', 'longrepr': None, 'result': [<Function test_important>], 'sections': []} # noqa: E501
320+
# def {'nodeid': 'tests/foo_test.py', 'outcome': 'passed', 'longrepr': None, 'result': [<Class TestDomain>], 'sections': []} # noqa: E501
321+
# def {'nodeid': 'tests', 'outcome': 'passed', 'longrepr': None, 'result': [<Module foo_test.py>], 'sections': []} # noqa: E501
322+
# def {'nodeid': '.', 'outcome': 'passed', 'longrepr': None, 'result': [<Dir tests>], 'sections': []} # noqa: E501
323+
# off {'nodeid': '', 'outcome': 'passed', 'longrepr': None, 'result': [<Dir test_hook_behaviour1>], 'sections': []} # noqa: E501
324+
# off {'nodeid': 'src', 'outcome': 'passed', 'longrepr': None, 'result': [], 'sections': []}
325+
# off {'nodeid': 'tests/foo_test.py::TestDomain', 'outcome': 'passed', 'longrepr': None, 'result': [<Function test_important>], 'sections': []} # noqa: E501
326+
# off {'nodeid': 'tests/foo_test.py', 'outcome': 'passed', 'longrepr': None, 'result': [<Class TestDomain>], 'sections': []} # noqa: E501
327+
# off {'nodeid': 'tests', 'outcome': 'passed', 'longrepr': None, 'result': [<Module foo_test.py>], 'sections': []} # noqa: E501
328+
# off {'nodeid': '.', 'outcome': 'passed', 'longrepr': None, 'result': [<Dir src>, <Dir tests>], 'sections': []} # noqa: E501
308329

309-
# from pprint import pprint
310-
# pprint(default['reports'])
311-
# pprint(collect_off['reports'])
312-
# assert default['reports'] == collect_off['reports']
330+
assert len(default["reports"]) == len(collect_off["reports"])

0 commit comments

Comments
 (0)