-
Notifications
You must be signed in to change notification settings - Fork 103
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
Parameterized doens't play nicely with unittest.mock.patch
decorator
#66
Comments
Well this parametrized test which tests |
I think the issue I'm running into only occurs if the test raises an exception. Edit: And looks like the root cause of the exception raised (in my instance) was the parameter ordering ( |
Good to know, thanks! That seems like a common stumbling block, so I've updated the documentation to include an example. |
I think this is still an issue as I can reproduce it even with the correct ordering of the variable. @parameterized.expand([("x", "y"),])
@patch("foo")
def test_bar(x, y, foo):
assertEqual(1,2) As expected it's only reproducible with py2 using the mocks backport. py3 works fine. |
I have the same issue with v0.7.0. The setup is following: @parameterized.expand([("x", "y"),])
@patch("internal.lib.timezone_at", new=mock_timezone_at('Europe/Zurich'))
def test_bar(x, y):
assertEqual(1,2) If context manager is used instead, everything works: @parameterized.expand([("x", "y"),])
def test_bar(x, y):
with patch("internal.lib.timezone_at", new=mock_timezone_at('Europe/Zurich')):
assertEqual(1,2) In other words, UnboundLocalError is thrown instead of the actual exception when patch is used as decorator together with parameterized. |
Hrm, I've added a test case for this, but I can't seem to recreate the issue: 85222cf I've also tried to reproduce it by raising an explicit
Could you try running this test with I'm wondering if it's an issue with a particular version of mock, or something similar. |
The new test passed with pytest3, however, I managed to recreate the same issue by adding @parameterized.expand([(42,)])
@mock.patch('os.umask', new=lambda x: x)
def test_mock_patch_standalone_function2(foo):
raise ValueError() into |
@korcek-juraj hrm… I'm still not able to reproduce the issue. When I use the example you've included, I get the correct stack trace:
@parameterized([(42, )])
@mock.patch("os.umask", new=lambda x: x)
def test_mock_patch_standalone_function(foo, mock_umask):
raise ValueError() To help me debug this further, could you:
Mine is:
|
@wolever When I run the test case given by you using
When I change
Output of
Do you want me to create PR adding the test that is failing? |
Yes, a PR would be helpful, thanks! |
Okay, PR #72 created. In Travis you can see that most of the builds fail with UnboundLocalError. |
Slightly tangential but could be relevant if there are doc updates here Personally, I've had so many problems with brittleness in mock-patching, vs. rock-solid results from pytest's So it may be worth noting that for pytest users, |
Oof, okay, I see the issue now. This is a good bug - nice find! And thanks for taking the time to submit the PR. Basically, the bug is happening because the patches are being copied to each expanded function by Would you be able to take a look the potential fix I've push'd and see if it works for you? Running the test suite shows the expected failure. |
I have tested it by running the test suite as well as the tests in my code and I confirm that it indeed works now. Thanks a lot for fixing it! |
@hangtwenty thanks for the insight |
I still see this error in 0.7.1. Hasn't it been fixed already? |
Can you please merge #72 and cut out a new release? |
Is this still unresolved? |
It seems so. I've just ran into it with 0.7.1 (Params are in the correct order: mock last) |
Unfortunately this is still unresolved. The PR in question - #72 - is just a demonstration of the bug, not an actual fix. |
My workaround was to use patch as a context manager:
|
I should mention I work around it by using |
This was a bug in |
Howdy!
I've found that mixing Python's
unittest.mock.patch
decorator withparameterized.expand
leads to unfortunate side-effects.Will lead to
UnboundLocalError: local variable 'patching' referenced before assignment
(atunittest\mock.py:1181
).I'm sure this is due to the nature of how
expand
generates test cases.There's a workaround, which is use
patch
as a context manager in the test body. However, I'd still love to use both as decorators for all that delicious syntactic sugar and saved whitespace 😄.The text was updated successfully, but these errors were encountered: