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

[undocumented] Pytest package fixtures only respect __init__.py-containing packages #13216

Open
4 tasks done
alfaix opened this issue Feb 11, 2025 · 0 comments
Open
4 tasks done

Comments

@alfaix
Copy link

alfaix commented Feb 11, 2025

Checklist:

  • 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

The problem:
Package fixtures are destroyed at the end of the pytest run, not at the end of the last test within the package, as the documentation says. This bug prevents different package-level fixtures from accessing the same resource (e.g., a docker container in my case).

The mitigation is to create an empty __init__.py, however as of Python3.3, this should not be mandatory (packages can be defined by the directory structure).

I put the repro in this repository for convenience, but it boils down to creating 2 tests, 1 module scope at top-level, and 1 at package level with a package-scope fixture defined in conftest. The fixture is destroyed after the toplevel test (incorrectly) if __init__.py is missing, otherwise it acts correctly (as implied by the doc + Python3 definition of a package at least) and is destroyed after the nested test.

The behavior should be either documented at the link above or changed.

Versions and such

Package        Version Editable project location
-------------- ------- -------------------------
exceptiongroup 1.2.2
iniconfig      2.0.0
package        0.1.0   /home/akitov/pytest-repro
packaging      24.2
pip            24.3.1
pluggy         1.5.0
pytest         8.3.4
tomli          2.2.1
$ uname -a
Linux LL8FZ1VT3 6.8.0-51-generic #52~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Dec  9 15:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant