| 1 |
"""Tests for the `collect_imported_tests` configuration value.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import textwrap |
| 6 |
|
| 7 |
from _pytest.pytester import Pytester |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
def setup_files(pytester: Pytester) -> None: |
| 12 |
src_dir = pytester.mkdir("src") |
| 13 |
tests_dir = pytester.mkdir("tests") |
| 14 |
src_file = src_dir / "foo.py" |
| 15 |
|
| 16 |
src_file.write_text( |
| 17 |
textwrap.dedent("""\ |
| 18 |
class Testament: |
| 19 |
def test_collections(self): |
| 20 |
pass |
| 21 |
|
| 22 |
def test_testament(): pass |
| 23 |
"""), |
| 24 |
encoding="utf-8", |
| 25 |
) |
| 26 |
|
| 27 |
test_file = tests_dir / "foo_test.py" |
| 28 |
test_file.write_text( |
| 29 |
textwrap.dedent("""\ |
| 30 |
from foo import Testament, test_testament |
| 31 |
|
| 32 |
class TestDomain: |
| 33 |
def test(self): |
| 34 |
testament = Testament() |
| 35 |
assert testament |
| 36 |
"""), |
| 37 |
encoding="utf-8", |
| 38 |
) |
| 39 |
|
| 40 |
pytester.syspathinsert(src_dir) |
| 41 |
|
| 42 |
|
| 43 |
def test_collect_imports_disabled(pytester: Pytester) -> None: |
| 44 |
""" |
| 45 |
When collect_imported_tests is disabled, only objects in the |
| 46 |
test modules are collected as tests, so the imported names (`Testament` and `test_testament`) |
| 47 |
are not collected. |
| 48 |
""" |
| 49 |
pytester.makeini( |
| 50 |
""" |
| 51 |
[pytest] |
| 52 |
collect_imported_tests = false |
| 53 |
""" |
| 54 |
) |
| 55 |
|
| 56 |
setup_files(pytester) |
| 57 |
result = pytester.runpytest("-v", "tests") |
| 58 |
result.stdout.fnmatch_lines( |
| 59 |
[ |
| 60 |
"tests/foo_test.py::TestDomain::test PASSED*", |
| 61 |
] |
| 62 |
) |
| 63 |
|
| 64 |
# Ensure that the hooks were only called for the collected item. |
| 65 |
reprec = result.reprec # type:ignore[attr-defined] |
| 66 |
reports = reprec.getreports("pytest_collectreport") |
| 67 |
[modified] = reprec.getcalls("pytest_collection_modifyitems") |
| 68 |
[item_collected] = reprec.getcalls("pytest_itemcollected") |
| 69 |
|
| 70 |
assert [x.nodeid for x in reports] == [ |
| 71 |
"", |
| 72 |
"tests/foo_test.py::TestDomain", |
| 73 |
"tests/foo_test.py", |
| 74 |
"tests", |
| 75 |
] |
| 76 |
assert [x.nodeid for x in modified.items] == ["tests/foo_test.py::TestDomain::test"] |
| 77 |
assert item_collected.item.nodeid == "tests/foo_test.py::TestDomain::test" |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.parametrize("configure_ini", [False, True]) |
| 81 |
def test_collect_imports_enabled(pytester: Pytester, configure_ini: bool) -> None: |
| 82 |
""" |
| 83 |
When collect_imported_tests is enabled (the default), all names in the |
| 84 |
test modules are collected as tests. |
| 85 |
""" |
| 86 |
if configure_ini: |
| 87 |
pytester.makeini( |
| 88 |
""" |
| 89 |
[pytest] |
| 90 |
collect_imported_tests = true |
| 91 |
""" |
| 92 |
) |
| 93 |
|
| 94 |
setup_files(pytester) |
| 95 |
result = pytester.runpytest("-v", "tests") |
| 96 |
result.stdout.fnmatch_lines( |
| 97 |
[ |
| 98 |
"tests/foo_test.py::Testament::test_collections PASSED*", |
| 99 |
"tests/foo_test.py::test_testament PASSED*", |
| 100 |
"tests/foo_test.py::TestDomain::test PASSED*", |
| 101 |
] |
| 102 |
) |