-
Notifications
You must be signed in to change notification settings - Fork 91
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
Pytest plugin has side effect and is extremely unintuitive to use #566
Comments
I agree with the behavior of the Not so sure about the fixture. This is the standard behavior of pytest and all pytest plugins. While that may be weird, it is the de-facto standard as more and more projects are using pytest nowadays. Changing that would break all the existing usage of the |
Wow, thanks a lot for responding so quickly!
Sure, but I would argue that this doesn't mean one should follow this weird standard. ;) Global state is never a good idea, as demonstrated perfectly by this issue.
Would a breaking change in an upcoming major version really be an issue, though, especially when adapting to the breaking change in existing projects would involve only changing one single line to the list of dependencies? (Besides, as I said, one could add a deprecation warning in the current major version of pyfakefs to further smoothen the transition process.) |
Well, I disagree. Changing the established standard for pytest is an uphill battle that I don't want to fight. And yes, global state is not a good idea, but in this case it is something that a criticial mass of developers is used to, so there you are... Anyway, I would like to hear the opinion of @jmcgeheeiv on this. Going the way of splitting the fixture into an extra package is something we might indeed consider. We could add a new repository ( |
Fair enough, the decision is certainly yours, not mine, to make. Thanks again for even taking the time to consider this! |
I will try to change the keyword argument to a positional argument, as this would certainly be better. I now remember why I didn't use a positional argument in the first place. While it is very easy to change, it does not play well with |
- avoids confudion with pytest fixture - see pytest-dev#566
Hm, I probably will just document the behavior - not much I can do here, as all |
Sorry for not responding!
Could elaborate on this? By "writing @patchfs before @mock.patch" do you mean
or
? In the first case I would expect |
Yes, but in reality it is vice verse now. |
Well, I think we are not going to do anything else here. In principle, a |
This might not be considered a bug, but it is – at the very least – a severe code smell.
Describe the bug
Once pyfakefs is installed, the pytest plugin seems to get loaded automatically and register a fixture with pytest. This has severe consequences:
fs
parameter is coming fromfrom pyfakefs.pytest_plugin import fs
, the import will seem un-used and linters and code formatters will complain about it or even remove it automatically.fs
fixtures in different tests. The pytest fixture that pyfakefs registers is global state.@patchfs
decorator on one of my pytest unit tests to make the pyfakefs dependency more obvious as@patchfs
uses the same named parameterfs
and employsfunctools.wraps
to make the wrapped function look like the original one (including its named parameters). (Attempting to use@patchfs
on a unit test results in a recursion error. Let me know if I should file a separate bug report for this.)I am aware that a large part of this issue is caused by pytest's weird approach of having a global registry of fixtures. However, the fixture in pyfakefs exacerbates this by orders of magnitude.
Suggestions:
pyfakefs.pytest_plugin
in a function that the user has to call explicitly somewhere in her/his code in order to register the fixture. (Clearly, this would be a breaking change.)@patchfs
and using fixtures is essentially global state. (This would be a breaking change, too.)@patchfs
pass the FakeFilesystem instance to the decorated function as the first parameter, instead of as a named parameterfs
. This way, one could still use@patchfs
on pytest unit tests without interfering with the fixture. (This could break some existing code.)While a breaking change seems little desirable, it is IMO the only real solution in the long term. One could try to smoothen the transition process by e.g. deprecating the fixture first.
The text was updated successfully, but these errors were encountered: