|
| 1 | +"""Regression test for autouse fixtures with doctest files. |
| 2 | +
|
| 3 | +Backported from pytest commit 9cd14b4ff (2024-02-06). |
| 4 | +https://github.com/pytest-dev/pytest/commit/9cd14b4ff |
| 5 | +
|
| 6 | +The original pytest test verified autouse fixtures defined in the same .py module |
| 7 | +as the doctest get picked up properly. For gp-libs, DocTestDocutilsFile handles |
| 8 | +.rst/.md files where fixtures can only come from conftest.py (not from the |
| 9 | +document itself). |
| 10 | +
|
| 11 | +This test verifies that autouse fixtures from conftest.py are properly discovered |
| 12 | +for .rst and .md doctest files across different fixture scopes. |
| 13 | +
|
| 14 | +Refs: pytest-dev/pytest#11929 |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import textwrap |
| 20 | +import typing as t |
| 21 | + |
| 22 | +import _pytest.pytester |
| 23 | +import pytest |
| 24 | + |
| 25 | + |
| 26 | +class AutouseFixtureTestCase(t.NamedTuple): |
| 27 | + """Test fixture for autouse fixtures with doctest files.""" |
| 28 | + |
| 29 | + test_id: str |
| 30 | + scope: str |
| 31 | + file_ext: str |
| 32 | + file_content: str |
| 33 | + |
| 34 | + |
| 35 | +RST_DOCTEST_CONTENT = textwrap.dedent( |
| 36 | + """ |
| 37 | +Example |
| 38 | +======= |
| 39 | +
|
| 40 | +.. doctest:: |
| 41 | +
|
| 42 | + >>> get_value() |
| 43 | + 'fixture ran' |
| 44 | + """, |
| 45 | +) |
| 46 | + |
| 47 | +MD_DOCTEST_CONTENT = textwrap.dedent( |
| 48 | + """ |
| 49 | +# Example |
| 50 | +
|
| 51 | +```{doctest} |
| 52 | +>>> get_value() |
| 53 | +'fixture ran' |
| 54 | +``` |
| 55 | + """, |
| 56 | +) |
| 57 | + |
| 58 | +SCOPES = ["module", "session", "class", "function"] |
| 59 | + |
| 60 | +FIXTURES = [ |
| 61 | + AutouseFixtureTestCase( |
| 62 | + test_id=f"{scope}-rst", |
| 63 | + scope=scope, |
| 64 | + file_ext=".rst", |
| 65 | + file_content=RST_DOCTEST_CONTENT, |
| 66 | + ) |
| 67 | + for scope in SCOPES |
| 68 | +] + [ |
| 69 | + AutouseFixtureTestCase( |
| 70 | + test_id=f"{scope}-md", |
| 71 | + scope=scope, |
| 72 | + file_ext=".md", |
| 73 | + file_content=MD_DOCTEST_CONTENT, |
| 74 | + ) |
| 75 | + for scope in SCOPES |
| 76 | +] |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + AutouseFixtureTestCase._fields, |
| 81 | + FIXTURES, |
| 82 | + ids=[f.test_id for f in FIXTURES], |
| 83 | +) |
| 84 | +def test_autouse_fixtures_with_doctest_files( |
| 85 | + pytester: _pytest.pytester.Pytester, |
| 86 | + test_id: str, |
| 87 | + scope: str, |
| 88 | + file_ext: str, |
| 89 | + file_content: str, |
| 90 | +) -> None: |
| 91 | + """Autouse fixtures from conftest.py work with .rst/.md doctest files. |
| 92 | +
|
| 93 | + Regression test for pytest-dev/pytest#11929. |
| 94 | + Backported from pytest commit 9cd14b4ff (2024-02-06). |
| 95 | + """ |
| 96 | + pytester.plugins = ["pytest_doctest_docutils"] |
| 97 | + pytester.makefile( |
| 98 | + ".ini", |
| 99 | + pytest=textwrap.dedent( |
| 100 | + """ |
| 101 | +[pytest] |
| 102 | +addopts=-p no:doctest -vv |
| 103 | + """.strip(), |
| 104 | + ), |
| 105 | + ) |
| 106 | + |
| 107 | + # Create conftest with autouse fixture that sets a global value |
| 108 | + pytester.makeconftest( |
| 109 | + textwrap.dedent( |
| 110 | + f""" |
| 111 | +import pytest |
| 112 | +
|
| 113 | +VALUE = "fixture did not run" |
| 114 | +
|
| 115 | +@pytest.fixture(autouse=True, scope="{scope}") |
| 116 | +def set_value(): |
| 117 | + global VALUE |
| 118 | + VALUE = "fixture ran" |
| 119 | +
|
| 120 | +@pytest.fixture(autouse=True) |
| 121 | +def add_get_value_to_doctest_namespace(doctest_namespace): |
| 122 | + def get_value(): |
| 123 | + return VALUE |
| 124 | + doctest_namespace["get_value"] = get_value |
| 125 | + """, |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + # Create the doctest file |
| 130 | + tests_path = pytester.path / "tests" |
| 131 | + tests_path.mkdir() |
| 132 | + test_file = tests_path / f"example{file_ext}" |
| 133 | + test_file.write_text(file_content, encoding="utf-8") |
| 134 | + |
| 135 | + result = pytester.runpytest(str(test_file)) |
| 136 | + result.assert_outcomes(passed=1) |
0 commit comments