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

@example decorator not working with patched test #2231

Closed
fulaphex opened this issue Nov 25, 2019 · 2 comments
Closed

@example decorator not working with patched test #2231

fulaphex opened this issue Nov 25, 2019 · 2 comments
Labels
bug something is clearly wrong here

Comments

@fulaphex
Copy link

Using @given, @example and @mock.patch on a test that is a method of a TestCase class fails with:

TypeError: test_bar() missing 1 required positional argument: 'self'

Code triggering the exception:

from hypothesis import given, example, strategies
from unittest import mock, TestCase

def bar():
    return 1


class FooTest(TestCase):
    @given(baz=strategies.integers())
    @example(baz=3)
    @mock.patch("foo.bar", lambda: 2)
    def test_bar(self, baz):
        pass
        

Full error:

============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.2.4, py-1.8.0, pluggy-0.13.0
rootdir: /tmp
plugins: hypothesis-4.47.0
collected 1 item

foo.py F                                                                 [100%]

=================================== FAILURES ===================================
_______________________________ FooTest.test_bar _______________________________

args = (<foo.FooTest testMethod=test_bar>,), keywargs = {}

    @wraps(func)
>   def patched(*args, **keywargs):

/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1194: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.7/site-packages/hypothesis/core.py:369: in execute_explicit_examples
    print_example=True,
/usr/local/lib/python3.7/site-packages/hypothesis/core.py:573: in execute_once
    result = self.test_runner(data, run)
/usr/local/lib/python3.7/site-packages/hypothesis/executors.py:56: in default_new_style_executor
    return function(data)
/usr/local/lib/python3.7/site-packages/hypothesis/core.py:569: in run
    return test(*args, **kwargs)
/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1194: in test_bar
    def patched(*args, **keywargs):
/usr/local/lib/python3.7/site-packages/hypothesis/core.py:533: in test
    result = self.test(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

args = (), keywargs = {'baz': 3}, extra_args = []
entered_patchers = [<unittest.mock._patch object at 0x10dcad090>]
exc_info = (<class 'TypeError'>, TypeError("test_bar() missing 1 required positional argument: 'self'"), <traceback object at 0x10dd1a280>)
patching = <unittest.mock._patch object at 0x10dcad090>
arg = <function FooTest.<lambda> at 0x10dc9be60>

    @wraps(func)
    def patched(*args, **keywargs):
        extra_args = []
        entered_patchers = []
    
        exc_info = tuple()
        try:
            for patching in patched.patchings:
                arg = patching.__enter__()
                entered_patchers.append(patching)
                if patching.attribute_name is not None:
                    keywargs.update(arg)
                elif patching.new is DEFAULT:
                    extra_args.append(arg)
    
            args += tuple(extra_args)
>           return func(*args, **keywargs)
E           TypeError: test_bar() missing 1 required positional argument: 'self'

/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1209: TypeError
---------------------------------- Hypothesis ----------------------------------
Falsifying example: test_bar(baz=3)
============================== 1 failed in 0.16s ===============================

Changing the order of the decorators, putting the @mock.patch as the outermost seems to solve the issue.
I have checked multiple versions of hypothesis, the newest version for which the code above works fine is 4.46.2, it's broken in 4.47.*.

@Zac-HD
Copy link
Member

Zac-HD commented Nov 25, 2019

Huh, sounds kinda like #961 but new as of #2216.

The fix will probably involve some awful argspec juggling, and possibly a special case to handle such decorators 😕

@Zac-HD Zac-HD added the bug something is clearly wrong here label Nov 25, 2019
@Zac-HD
Copy link
Member

Zac-HD commented Sep 13, 2021

Following #3088 and #3095, I'm confident that - unfortunately! - we can't fix this on in Hypothesis; it's a more generic interaction between the behaviour of definition-time nested decorators and the self argument in bound methods.

@Zac-HD Zac-HD closed this as completed Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

No branches or pull requests

2 participants