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

Improve documentation around conftest-files and importing of fixtures #13148

Open
The-Compiler opened this issue Jan 20, 2025 · 1 comment
Open
Labels
type: docs documentation improvement, missing or needing clarification

Comments

@The-Compiler
Copy link
Member

Prompted by #13144, I looked at the fixtures documentation of pytest again, and I feel like we're not doing a very good job at explaining a quite basic concept: How to use conftest.py files on a basic level, and why fixtures shouldn't be imported. I'd even go as far as recommending a rule of thumb like "never import anything from a test file or from a conftest.py, unless it's for type annotations".

The section about scope could then perhaps have a note linking to that, to avoid confusion when something like in #13144 happens.

What we have so far:

How to use fixtures mentions conftest.py in passing, while actually talking about the scope argument (which seems extra confusing, even more so with the title of "scope: sharing fixtures", while scope is about the caching/lifetime and not the visibility!):

The next example puts the fixture function into a separate conftest.py file so that tests from multiple test modules in the directory can access the fixture function: [...]

In the same document, under Using fixtures from other projects, there is a note (emphasis mine):

Sometimes users will import fixtures from other projects for use, however this is not recommended: importing fixtures into a module will register them in pytest as defined in that module.

This has minor consequences, such as appearing multiple times in pytest --help, but it is not recommended because this behavior might change/stop working in future versions.

While the note is aimed at importing things from other non-test code, as seen in #13144, doing something very similar in the current's project test code can introduce confusion on why a fixture with scope="session" that's imported somewhere else is run twice.

Finally, only in the fixtures reference we have a section about conftest.py, which already goes into quite some technical detail with how nesting of multiple conftest.py works.

@The-Compiler The-Compiler added the type: docs documentation improvement, missing or needing clarification label Jan 20, 2025
@andyDoucette
Copy link

andyDoucette commented Jan 21, 2025

Thank you for opening this, @The-Compiler! Right now it seems to me like we're trying to paint over a flaw in the design, but maybe once I read the new docs I'll understand why you guys did it the way you did.

Just for fun, can you also give equal thought to the "just change the behavior to be what python developers expect based on what they know about how python works" and weigh the pros and cons of both approaches? Sometimes changing the code to make it do the intuitive thing can be better than documenting the heck out of the non-intuitive thing. I can't say for sure if it's better, though, since I don't know the details of why it was designed the way it was - only someone with more experience in the pytest internals would know the challenges that needed to be overcome. I just know it's really counter-intuitive from a user standpoint. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs documentation improvement, missing or needing clarification
Projects
None yet
Development

No branches or pull requests

2 participants