-
Notifications
You must be signed in to change notification settings - Fork 586
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
given's interaction with mock.patch decorator is broken in almost every way #198
Comments
|
Note: Let me know if you really really care about this bug, because the combination of "good workarounds", "all the fixes are awful and/or partial" and "I don't actually like mocking" means that unless this is actually bothering people I am very unmotivated to work on it. |
|
Thanks for the details on this. Fixing this is not really important to me, I don't think the workarounds are burdensome at all. I've been using pytest and hypothesis for years and this has never been an issue before. I was just perplexed at the behavior but I understand the reasons it happens better now. |
|
@DRMacIver I ran into this same issue (i.e. the second of the two reported problems) when injecting mocks using By hacking some code in Thanks! |
Hypothesis is full of hacky workarounds for weird behaviour, so no objections on that front. :-) Very happy to accept a pull request for this. The other thing I thought about but never got around to investigating as a way of detecting mocks is to check for the presence of an arbitrary attribute. e.g. if A thing worth noting BTW is that pytest fixtures also interact unexpectedly with Hypothesis (function level fixtures are once for the whole test, not once per example), so you may run into problems on that front too. Fixing this one is a rather larger task though (or requires work on pytest's side). |
|
Alright @DRMacIver , so I made a pull request to address a portion of this issue: #779. As you mentioned, this is not really going to do much in relation to the other challenges re: fixtures, but it's an improvement nonetheless. I wonder - should this bug (198) be broken down into smaller issues? Seems kind of overwhelming in its current form due to its scope. |
@waynew is trying to use unitest.mock's patch decorator together with Hypothesis's given and py.test's fixtures. He's not having a very good time of it, which is unsurprising because this combination almost entirely doesn't work.
The first way it goes wrong:
You can mostly use mock.patch with given if you don't care about py.test fixtures. For example, the following will work:
But if you try to add an extra named argument then everything will go wrong: Hypothesis will expose something with just _args and *_kwargs because that's what mock.patch gives it, so py.test won't be able to find the fixture.
It's unclear that there is a correct thing Hypothesis could do at this point. mock.patch is using functools.wrap, so in Python 3 we could propagate the wrapped attribute, but AFAIK there's no equivalent in Python 2. Additionally, note that the signature of the wrapped function is not correct, because the first arguments are filled in manually. Historically, propagating wrapped has broken py.test (pytest-dev/pytest#677), but this may be affected differently now?
Anyway: Can of worms, unclear there's a correct thing to do here.
The second way it goes wrong is more amusing and is probably fixable (update: was fixed in #779), or at least can be mitigated:
This test passes.
Why does this test pass? It passes because Hypothesis can't currently tell the difference between being called as a bound method and just having an argument explicitly passed to it as the first positional argument. This means that it thinks that the mock is self and begins the shenanigans for the executor API. It looks for a method called execute_example, finds it, and then calls it with the test function.
It then of course immediately returns, never executing the test.
Possible ways to work around:
The text was updated successfully, but these errors were encountered: