Skip to content

Commit 022d316

Browse files
committed
Ensure correct collection of reports
1 parent f1821ea commit 022d316

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

testing/test_collect_imports.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from _pytest.pytester import Pytester
99
from _pytest.pytester import RecordedHookCall
1010
from _pytest.pytester import RunResult
11+
from _pytest.reports import CollectReport
1112
import pytest
1213

1314

@@ -71,15 +72,19 @@ def test_collect_imports_disabled(pytester: Pytester) -> None:
7172

7273
# Verify that the state of hooks
7374
reprec = pytester.inline_run()
75+
reports = reprec.getreports("pytest_collectreport")
76+
modified = reprec.getcalls("pytest_collection_modifyitems")
7477
items_collected = reprec.getcalls("pytest_itemcollected")
78+
79+
assert len(reports) == 5
80+
assert len(modified) == 1
7581
assert len(items_collected) == 1
7682
for x in items_collected:
7783
assert x.item._getobj().__name__ == "test_testament"
7884

7985

8086
def test_collect_imports_default(pytester: Pytester) -> None:
8187
run_import_class_test(pytester, errors=1)
82-
# TODO
8388

8489

8590
def test_collect_imports_enabled(pytester: Pytester) -> None:
@@ -260,7 +265,6 @@ def _test_hook_behaviour_when_collect_off(self, pytester: Pytester) -> None:
260265
}
261266

262267
def _test_hook_behaviour(self) -> None:
263-
print("ABCD", self.collect_outcomes)
264268
default = self.collect_outcomes["default"]
265269
collect_off = self.collect_outcomes["collect_off"]
266270

@@ -294,37 +298,20 @@ def _test_hook_behaviour(self) -> None:
294298
off_items = off_items_record.__dict__["item"]
295299
assert def_items.name == off_items.name
296300

297-
# TODO: fix diff: (This will get cleaned up)
298-
# [
299-
# <CollectReport '' lenresult=1 outcome='passed'>,
300-
# - <CollectReport 'src' lenresult=0 outcome='passed'>,
301-
# <CollectReport 'tests/foo_test.py::TestDomain' lenresult=1 outcome='passed'>,
302-
# <CollectReport 'tests/foo_test.py' lenresult=1 outcome='passed'>,
303-
# <CollectReport 'tests' lenresult=1 outcome='passed'>,
304-
# - <CollectReport '.' lenresult=2 outcome='passed'>,
305-
# ? ^
306-
# + <CollectReport '.' lenresult=1 outcome='passed'>,
307-
# ? ^
308-
# ]
309-
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
329-
330-
assert len(default["reports"]) == len(collect_off["reports"])
301+
def compare_report(r1: CollectReport, r2: CollectReport) -> None:
302+
assert r1.result[0].name == r2.result[0].name
303+
assert len(r1.result) == len(r2.result)
304+
assert r1.outcome == r2.outcome
305+
306+
# Function test_important
307+
compare_report(default["reports"][1], collect_off["reports"][2])
308+
# Class TestDomain
309+
compare_report(default["reports"][2], collect_off["reports"][3])
310+
# Module foo_test.py
311+
compare_report(default["reports"][3], collect_off["reports"][4])
312+
313+
# + 1 since src dir is collected
314+
assert len(default["reports"]) + 1 == len(collect_off["reports"])
315+
316+
# Two Dirs will be collected, src and test.
317+
assert len(collect_off["reports"][5].result) == 2

0 commit comments

Comments
 (0)