Skip to content

Conversation

@Dooopinder
Copy link
Contributor

This PR clarifies fixture visibility vs scope, recommends conftest.py for sharing fixtures, and documents why importing fixtures is discouraged.

Clarifies that fixture scope controls lifetime and caching, not visibility

Explicitly explains that fixture visibility depends on where it is defined (test module, conftest.py, or plugin)

Recommends conftest.py as the correct and supported way to share fixtures across multiple test modules

Adds a clear warning against importing fixtures from test files or conftest.py, which can cause duplicate registration and confusing behavior

Adds a simple rule of thumb: never import fixtures except for type annotations

Links readers to the reference documentation for deeper technical details

closes #13148

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jan 18, 2026
Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this. Please see my comments.

Also pinging @The-Compiler who might want to review this (the issue discusses a bit larger reworking of the docs around this IIUC).

Possible values for ``scope`` are: ``function``, ``class``, ``module``, ``package`` or ``session``.

The next example puts the fixture function into a separate ``conftest.py`` file
.. note::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try integrating this into the text itself rather than in a "note"? The "note" structure somewhat interrupts the flow and it's tempting to have too many of them but I think it's better to have a natural flow.

The next example puts the fixture function into a separate ``conftest.py`` file
.. note::

Fixture *scope* controls lifetime and caching, not visibility.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first usage of the word "visibility", it will be hard to understand the meaning of the sentence without already knowing what it means in this context.


Fixture *scope* controls lifetime and caching, not visibility.
Visibility is determined by where the fixture is defined (test module,
``conftest.py``, or plugin).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conftests are first mentioned in the next sentence, so in terms of flow it will be better to discuss this after.


.. note::

This is the recommended way to share fixtures across multiple test modules.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit better to avoid "This" and the thing itself instead, i.e. "Conftests are the recommended way ...". This (hehe) way the paragraph stands on its own.

This is the recommended way to share fixtures across multiple test modules.
Avoid importing fixtures from other test files or from ``conftest.py``,
as importing fixtures can cause them to be registered in multiple modules and
lead to confusing behavior (such as fixtures running more than once).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lead to confusing behavior (such as fixtures running more than once).
lead to confusing behavior such as fixtures running more than once.

lead to confusing behavior (such as fixtures running more than once).

As a rule of thumb: **never import fixtures from test files or conftest.py,
unless it is only for type annotations**.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the type annotations part?

@@ -0,0 +1 @@
Clarified fixture visibility, conftest usage, and why importing fixtures is discouraged.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Clarified fixture visibility, conftest usage, and why importing fixtures is discouraged.
:ref:`Clarified <smtpshared>` fixture visibility, conftest usage, and why importing fixtures is discouraged.

@Dooopinder
Copy link
Contributor Author

Thanks a lot for the detailed review! This makes sense — I’ll rework the text

@Dooopinder
Copy link
Contributor Author

Hello @bluetech :) I’ve updated the text to integrate the notes into the main flow, clarified the wording, and addressed the type-annotation explanation. Please let me know if this looks better now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve documentation around conftest-files and importing of fixtures

2 participants