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

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

Closed
4 tasks done
spearsem opened this issue Jul 8, 2019 · 7 comments
Labels
type: bug problem that needs to be addressed type: docs documentation improvement, missing or needing clarification

Comments

@spearsem
Copy link

spearsem commented Jul 8, 2019

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 that monkeypatch 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 the requests attribute from the requests 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.

  • a detailed description of the bug or suggestion
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible
@RonnyPfannschmidt
Copy link
Member

in pytest 3.0 there was a experimental feature for multi scoped fixtures,
we did later learn that the tech debt in the fixture system made the implementation do incorrect/bad things, and we had to take it out as it broke in too many/surprising ways,

unfortunately it doesn't seem to be in the change-log - @nicoddemus any idea what happened to the historic record?

@RonnyPfannschmidt
Copy link
Member

#1811

@spearsem
Copy link
Author

spearsem commented Jul 8, 2019

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.

@RonnyPfannschmidt
Copy link
Member

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

@nicoddemus
Copy link
Member

nicoddemus commented Jul 8, 2019

it got pulled right before the release and i believe there where oversights in the docs that have since been fixed

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.

i wonder how to correctly mark those as deprecated/broken

If everyone agrees, we can just take those docs down. 👍

@nicoddemus nicoddemus added type: bug problem that needs to be addressed type: docs documentation improvement, missing or needing clarification labels Jul 8, 2019
@RonnyPfannschmidt
Copy link
Member

Take down is fine

@nicoddemus
Copy link
Member

Done! Closing this for now then, thanks everyone. 👍

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

No branches or pull requests

3 participants