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

Patching (mock) fails if custom patch value is provided with mock.patch/mock.patch.object #39

Closed
debayanray opened this issue Jan 11, 2016 · 2 comments

Comments

@debayanray
Copy link

I am running a test case under a test class as:

@ddt.ddt
class IloCommonModuleTestCase(unittest.TestCase):

    @mock.patch.object(time, 'sleep', lambda x: None)
    def test_case1(self, sleep_mock):
        pass

    @ddt.data(
          ('data1_to_test_case'),
          ('data2_to_test_case'),
          ...,
     )
    @ddt.unpack
    def test_case2(self, test_sample_data):
        pass

wherein I have other test cases which uses ddt.data and ddt.unpack as depicted. Now when I run the test cases it fails in the test_case1 and I believe this i something to do with ddt:

Traceback (most recent call last):
  File ".../local/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
TypeError: test_case1() takes exactly 2 arguments (1 given)

But if I remove the custom value in the patching decorator, then it works fine.

    @mock.patch.object(time, 'sleep')
    def test_case1(self, sleep_mock):
        pass

Hence, I feel there's something which is missing, like you can provide custom value to patch decorators with mock, while the class under test gets decorated by @ddt.ddt Thank you.

@txels
Copy link
Collaborator

txels commented Jan 11, 2016

My guess is that this has nothing to do with ddt, and you are just using @mock.patch.object incorrectly. When you use the 3-arg version of patch.object, it won't pass a mock as an additional argument to your function, since it doesn't create one.

See http://www.voidspace.org.uk/python/mock/patch.html#patch-object for details.

Have you tried just removing the ddt class decorator and seeing how it fails?

@debayanray
Copy link
Author

thank you very much, @txels
yes, you were right. I was wrong in interpreting and was using the 3rd argument with patchers in a wrong manner. Thanks for pin-pointing the issue.
👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants