Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest 8.0.0rc1 stopped respecting usefixtures marker applied in pytest_itemcollected #11759

Closed
4 tasks done
flying-sheep opened this issue Jan 2, 2024 · 5 comments
Closed
4 tasks done
Labels
plugin: doctests related to the doctests builtin plugin type: bug problem that needs to be addressed
Milestone

Comments

@flying-sheep
Copy link
Contributor

flying-sheep commented Jan 2, 2024

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

In Pytest 7, adding a pytest.mark.usefixtures marker to a pytest.Item in pytest_itemcollected resulted in that marker being respected. With Pytest 8, that’s not the case.

The reproducer sets up two environments, one with Pytest 7.x, one with Pytest 8.0.0rc1, and runs a test suite:

reproducer

$ hatch run test
─────────────────────────────────────── 7 ───────────────────────────────────────
============================== test session starts ==============================
platform linux -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- .../7/bin/python
cachedir: .pytest_cache
rootdir: .../pytest-8-doctests
collected 2 items                                                               

test_warnings.py::test_warnings.has_doctest PASSED                        [ 50%]
test_warnings.py::test_warn PASSED                                        [100%]

=============================== 2 passed in 0.01s ===============================
─────────────────────────────────────── 8 ───────────────────────────────────────
============================== test session starts ==============================
platform linux -- Python 3.11.6, pytest-8.0.0rc1, pluggy-1.3.0 -- .../8/bin/python
cachedir: .pytest_cache
rootdir: .../pytest-8-doctests
collected 2 items                                                               

test_warnings.py::test_warnings.has_doctest FAILED                        [ 50%]
test_warnings.py::test_warn PASSED                                        [100%]

=================================== FAILURES ====================================
______________________ [doctest] test_warnings.has_doctest ______________________
007 Doctest!
008     
009     >>> import warnings
010     >>> warnings.warn("Hi!", FutureWarning)
UNEXPECTED EXCEPTION: FutureWarning('Hi!')
Traceback (most recent call last):
  File "/usr/lib/python3.11/doctest.py", line 1351, in __run
    exec(compile(example.source, filename, "single",
  File "<doctest test_warnings.has_doctest[1]>", line 1, in <module>
FutureWarning: Hi!
.../pytest-8-doctests/test_warnings.py:10: UnexpectedException
============================ short test summary info ============================
FAILED test_warnings.py::test_warnings.has_doctest
========================== 1 failed, 1 passed in 0.01s ==========================
@flying-sheep flying-sheep changed the title Pytest 8.0.0rc0 stopped respecting usefixtures marker applied in pytest_itemcollected Pytest 8.0.0rc1 stopped respecting usefixtures marker applied in pytest_itemcollected Jan 2, 2024
@bluetech bluetech added this to the 8.0 milestone Jan 2, 2024
@bluetech bluetech added the type: bug problem that needs to be addressed label Jan 2, 2024
@bluetech
Copy link
Member

bluetech commented Jan 2, 2024

Thanks for trying the release candidate! Will debug this soon.

@bluetech
Copy link
Member

bluetech commented Jan 2, 2024

Bisected to ab63ebb.

@bluetech bluetech added the plugin: doctests related to the doctests builtin plugin label Jan 2, 2024
@bluetech
Copy link
Member

bluetech commented Jan 2, 2024

The commit made doctest items more similar to function items, which (unintentionally) had this effect. This is to say, adding usefixtures in pytest_itemcollected has never worked for function items, now it also doesn't work for doctest items.

Now the FuncFixtureInfo is fetched when the item is created (which is before pytest_itemcollected). This is what checks the usefixture marks. Previously getfixtureinfo was called on the doctest item's setup (after collection entirely).

Ideally this issue is resolved for both types of items.

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Jan 3, 2024

The commit made doctest items more similar to function items

sounds like a great idea, I had quite some problems with conflicts due to double importing modules. Would be great if #11475 could be fixed for 8.0 to nail down doctests once and for all.

adding usefixtures in pytest_itemcollected has never worked for function items, now it also doesn't work for doctest items.

of course the least surprising thing from an API standpoint would be to make it just work for all items, but as an alternative, having a blessed way to hook into doctest setup and teardown would also be great!

@bluetech
Copy link
Member

@flying-sheep Instead of using the hook, could you maybe do this?

@pytest.fixture(autouse=True)
def suppress_env_for_doctests(request: pytest.FixtureRequest) -> None:
    if isinstance(request.node, pytest.DoctestItem):
        request.getfixturevalue("supress_env")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: doctests related to the doctests builtin plugin type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

2 participants