-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
In pytest 5.0.0 scope='session' does not work with monkeypatch, but it did in 3.0.0 and the docs are unclear. #5574
Comments
in pytest 3.0 there was a experimental feature for multi scoped fixtures, unfortunately it doesn't seem to be in the change-log - @nicoddemus any idea what happened to the historic record? |
It doesn't appear to be a previous 'experimental' feature, but rather it was a primary intended use case and had a dedicated section of documentation. If it was actually experimental, it might be good to retroactively add that tag to the documentation block, because it reads in 3.0.0 like a fully supported, first-class feature. Amending changelogs probably won't be enough to help people understand when searching online to find why it went away when upgrading or something. |
I did indeed misstemember some details, it got pulled right before the release and i believe there where oversights in the docs that have since been fixed, i wonder how to correctly mark those as deprecated/broken |
Exactly what happened: before the release we noticed the broken behavior in @The-Compiler's test suite, and reverted the feature completely, so it was never actually released. Hmm in retrospect, the problem might be more related to #4871 than the actual caching mechanism.
If everyone agrees, we can just take those docs down. 👍 |
Take down is fine |
Done! Closing this for now then, thanks everyone. 👍 |
If your try to create a session-scoped fixture using
monkeypatch
in pytest 5.0.0, like in a conftest.py file to patch an environment variable consumed by some other module at module import time, you get an error stating thatmonkeypatch
is a function-scoped fixture and and cannot be used for a session.However, in the docs for pytest 3.0.0, there is an explicit section of the docs recommending how to use
monkeypatch
as a session-scoped fixture.Since this same use case is not addressed at all in the docs for v 5.0.0, it means the 3.0.0 docs still show up highly in search engines when looking for this type of use case of monkeypatch.
It would probably be good to explicitly call out this functionality in both the 3.0.0 docs and 5.0.0 docs (and any intermediate docs versions too), with a special box indicating that this is not supported with newer pytest versions.
If possible, providing an example for how to solve the same type of problem using
monkeypatch
in v 5.0.0 would be great. Note that none of the existing examples explain how it could be solved, since they all focus on function-scoped monkeypatching. For example, there is an example about deleting therequests
attribute from therequests
module, but this does not address what happens if side-effectful behavior happens at import time based on a variable that needs to be patched.However, many times you will work with a Python module that sets up a module-level variable one time upon first import (like a logging setting or a path to a special data set to use depending on the environment).
How do we use
monkeypatch
in v 5.0.0 to solve that case and ensure the patching happens before a module is imported by a test and is cleaned up after that test session ends?I added a toy example in this comment on an old related issue.
pip list
from the virtual environment you are usingThe text was updated successfully, but these errors were encountered: