-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
mocker fixture cannot be used in module scope #136
Comments
Hi @aleGpereira, This is a pytest limitation I'm afraid, A proposal for a new "invocation-scoped" fixture is proposed in pytest-dev/pytest#1681, but the implementation for that has been pulled back because we encountered some severe problems with the implementation right before a release. For your module-scope fixture, an alternative is to fallback is the Cheers, |
A major reason for using mocker is to avoid 4-level deep "with" blocks when patching multiple functions; often using "with" is not very nice. I use the following trick: instead of using nested "with" statements, I define a nest function because I can then define the "mock.patch" as decorators. So instead of:
I use this:
This is better but the nice thing with mocker, is you don't have to worry about additional call params which you may not even care about (often _1, _2 etc are not used). So it would be nice to enable mocker to be used in different scopes. How about a mocker_each_func, mocker_each_cls, mocker_each_mod, etc? Just define a few additional mockers, one for each scope, might be simpler than one mocker that adapts. |
A simple solution to this could be to provide |
Greetings! Is there any progress with this one? |
Hi @doandzhiFt, Not really I'm afraid, but I will gladly review and merge a PR that adds |
While the feature proposed in pytest-dev/pytest#1681 is interesting and would serve as base for a more elegant solution to pytest-dev#136, adding those supplementary mockers allows this plugin to be used in other scopes. Fix pytest-dev#136
While the feature proposed in pytest-dev/pytest#1681 is interesting and would serve as a base for a more elegant solution to pytest-dev#136, adding those supplementary mockers allows this plugin to be used in other scopes. Fix pytest-dev#136
Ping, @nicoddemus |
Hi @scorphus, sorry for the delay. I've just merged your patch and will make a release shortly, thanks a lot for the contribution, I appreciate it greatly. 👍 |
Published in |
Great work, thanks guys! |
Glad it's useful to you too, @aleGpereira! Thanks again for the new release, @nicoddemus! Open source is amazing! Just a few days ago I tried to use |
I had the same problem. according to this issue, fixed it with |
It would be good to mention this in the docs - I'll try to open a PR when I have some time. |
Actually, it's already in there, I just somehow missed it (twice?): Lines 39 to 45 in e023728
|
If i define a custom fixture like:
@pytest.fixture(scope='module')
def my_fixture(mocker):
// code using mocker
class TestMyModule(object):
When i run this i get:
ScopeMismatch: You tried to access the 'function' scoped fixture 'mocker' with a 'module' scoped request object, involved factories
I'm i doing something wrong to do something like this? is there some reason why this is not supported?
Thanks.
The text was updated successfully, but these errors were encountered: