pytest-dev / pytest-dev/pytest

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

Open
#13,216 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked pytest-repro repository and compare package fixture teardown with and without an empty init.py. Read the fixture-scope documentation and inspect the conftest.py setup; done means the documented package scope and observed teardown behavior agree for namespace packages, either through a fix or clarified documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.