|
| 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_import_class_test(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 | + testpaths = "tests" |
| 53 | + collect_imported_tests = false |
| 54 | + """ |
| 55 | + ) |
| 56 | + |
| 57 | + setup_import_class_test(pytester) |
| 58 | + result = pytester.runpytest("-v") |
| 59 | + result.stdout.fnmatch_lines( |
| 60 | + [ |
| 61 | + "tests/foo_test.py::TestDomain::test PASSED*", |
| 62 | + ] |
| 63 | + ) |
| 64 | + |
| 65 | + # Ensure that the hooks were only called for the collected item. |
| 66 | + reprec = result.reprec # type:ignore[attr-defined] |
| 67 | + reports = reprec.getreports("pytest_collectreport") |
| 68 | + [modified] = reprec.getcalls("pytest_collection_modifyitems") |
| 69 | + [item_collected] = reprec.getcalls("pytest_itemcollected") |
| 70 | + |
| 71 | + assert [x.nodeid for x in reports] == [ |
| 72 | + "", |
| 73 | + "tests/foo_test.py::TestDomain", |
| 74 | + "tests/foo_test.py", |
| 75 | + "tests", |
| 76 | + ] |
| 77 | + assert [x.nodeid for x in modified.items] == ["tests/foo_test.py::TestDomain::test"] |
| 78 | + assert item_collected.item.nodeid == "tests/foo_test.py::TestDomain::test" |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("configure_ini", [False, True]) |
| 82 | +def test_collect_imports_enabled(pytester: Pytester, configure_ini: bool) -> None: |
| 83 | + """ |
| 84 | + When collect_imported_tests is enabled (the default), all names in the |
| 85 | + test modules are collected as tests. |
| 86 | + """ |
| 87 | + if configure_ini: |
| 88 | + pytester.makeini( |
| 89 | + """ |
| 90 | + [pytest] |
| 91 | + collect_imported_tests = true |
| 92 | + """ |
| 93 | + ) |
| 94 | + |
| 95 | + setup_import_class_test(pytester) |
| 96 | + result = pytester.runpytest("-v") |
| 97 | + result.stdout.fnmatch_lines( |
| 98 | + [ |
| 99 | + "tests/foo_test.py::Testament::test_collections PASSED*", |
| 100 | + "tests/foo_test.py::test_testament PASSED*", |
| 101 | + "tests/foo_test.py::TestDomain::test PASSED*", |
| 102 | + ] |
| 103 | + ) |
0 commit comments