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

Test failures in test_assert_called_args_with_introspection #102

Closed
ghisvail opened this issue Feb 15, 2018 · 13 comments
Closed

Test failures in test_assert_called_args_with_introspection #102

ghisvail opened this issue Feb 15, 2018 · 13 comments

Comments

@ghisvail
Copy link
Contributor

Whilst attempting to integrate pytest-mock to the Debian CI infrastructure, I encountered the following issue when running the tests against the installed version from the Debian package:

=================================== FAILURES ===================================
__________________ test_assert_called_args_with_introspection __________________

mocker = <pytest_mock.MockFixture object at 0x7f46460fce10>

    def test_assert_called_args_with_introspection(mocker):
        stub = mocker.stub()
    
        complex_args = ('a', 1, set(['test']))
        wrong_args = ('b', 2, set(['jest']))
    
        stub(*complex_args)
        stub.assert_called_with(*complex_args)
        stub.assert_called_once_with(*complex_args)
    
        with assert_argument_introspection(complex_args, wrong_args):
            stub.assert_called_with(*wrong_args)
>           stub.assert_called_once_with(*wrong_args)

test_pytest_mock.py:390: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python2.7/contextlib.py:35: in __exit__
    self.gen.throw(type, value, traceback)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

left = ('a', 1, set(['test'])), right = ('b', 2, set(['jest']))

    @contextmanager
    def assert_argument_introspection(left, right):
        """
        Assert detailed argument introspection is used
        """
        try:
            yield
        except AssertionError as e:
            # this may be a bit too assuming, but seems nicer then hard-coding
            import _pytest.assertion.util as util
            # NOTE: we assert with either verbose or not, depending on how our own
            #       test was run by examining sys.argv
            verbose = any(a.startswith('-v') for a in sys.argv)
            expected = '\n  '.join(util._compare_eq_iterable(left, right, verbose))
>           assert expected in str(e)
E           assert "Full diff:\n  - ('a', 1, set(['test']))\n  ?   ^   ^        ^\n  + ('b', 2, set(['jest']))\n  ?   ^   ^        ^" in "Expected call: mock('b', 2, set(['jest']))\nActual call: mock('a', 1, set(['test']))\n\npytest introspection follows:\n\nArgs:\n"
E            +  where "Expected call: mock('b', 2, set(['jest']))\nActual call: mock('a', 1, set(['test']))\n\npytest introspection follows:\n\nArgs:\n" = str(AssertionError(u"Expected call: mock('b', 2, set(['jest']))\nActual call: mock('a', 1, set(['test']))\n\npytest introspection follows:\n\nArgs:\n",))

test_pytest_mock.py:346: AssertionError
_________________ test_assert_called_kwargs_with_introspection _________________

mocker = <pytest_mock.MockFixture object at 0x7f464627c150>

    def test_assert_called_kwargs_with_introspection(mocker):
        stub = mocker.stub()
    
        complex_kwargs = dict(foo={'bar': 1, 'baz': 'spam'})
        wrong_kwargs = dict(foo={'goo': 1, 'baz': 'bran'})
    
        stub(**complex_kwargs)
        stub.assert_called_with(**complex_kwargs)
        stub.assert_called_once_with(**complex_kwargs)
    
        with assert_argument_introspection(complex_kwargs, wrong_kwargs):
            stub.assert_called_with(**wrong_kwargs)
>           stub.assert_called_once_with(**wrong_kwargs)

test_pytest_mock.py:405: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python2.7/contextlib.py:35: in __exit__
    self.gen.throw(type, value, traceback)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

left = {'foo': {'bar': 1, 'baz': 'spam'}}
right = {'foo': {'baz': 'bran', 'goo': 1}}

    @contextmanager
    def assert_argument_introspection(left, right):
        """
        Assert detailed argument introspection is used
        """
        try:
            yield
        except AssertionError as e:
            # this may be a bit too assuming, but seems nicer then hard-coding
            import _pytest.assertion.util as util
            # NOTE: we assert with either verbose or not, depending on how our own
            #       test was run by examining sys.argv
            verbose = any(a.startswith('-v') for a in sys.argv)
            expected = '\n  '.join(util._compare_eq_iterable(left, right, verbose))
>           assert expected in str(e)
E           assert "Full diff:\n  - {'foo': {'bar': 1, 'baz': 'spam'}}\n  + {'foo': {'baz': 'bran', 'goo': 1}}" in "Expected call: mock(foo={'goo': 1, 'baz': 'bran'})\nActual call: mock(foo={'baz': 'spam', 'bar': 1})\n\npytest introspection follows:\n\nKwargs:\n"
E            +  where "Expected call: mock(foo={'goo': 1, 'baz': 'bran'})\nActual call: mock(foo={'baz': 'spam', 'bar': 1})\n\npytest introspection follows:\n\nKwargs:\n" = str(AssertionError(u"Expected call: mock(foo={'goo': 1, 'baz': 'bran'})\nActual ca...foo={'baz': 'spam', 'bar': 1})\n\npytest introspection follows:\n\nKwargs:\n",))

test_pytest_mock.py:346: AssertionError
_________________________ test_detailed_introspection __________________________

testdir = <Testdir local('/tmp/pytest-of-root/pytest-0/testdir/test_detailed_introspection0')>

    def test_detailed_introspection(testdir):
        """Check that the "mock_use_standalone" is being used.
        """
        testdir.makepyfile("""
            def test(mocker):
                m = mocker.Mock()
                m('fo')
                m.assert_called_once_with('', bar=4)
        """)
        result = testdir.runpytest('-s')
        result.stdout.fnmatch_lines([
            "*AssertionError: Expected call: mock('', bar=4)*",
            "*Actual call: mock('fo')*",
            "*pytest introspection follows:*",
            '*Args:',
            "*assert ('fo',) == ('',)",
            "*At index 0 diff: 'fo' != ''*",
            "*Use -v to get the full diff*",
            "*Kwargs:*",
            "*assert {} == {'bar': 4}*",
            "*Right contains more items:*",
            "*{'bar': 4}*",
>           "*Use -v to get the full diff*",
        ])
E       Failed: nomatch: "*AssertionError: Expected call: mock('', bar=4)*"
E           and: u'============================= test session starts =============================='
E           and: u'platform linux2 -- Python 2.7.14+, pytest-3.2.5, py-1.4.34, pluggy-0.4.0'
E           and: u'rootdir: /tmp/pytest-of-root/pytest-0/testdir/test_detailed_introspection0, inifile:'
E           and: u'plugins: mock-1.6.3'
E           and: u'collected 1 item'
E           and: u''
E           and: u'test_detailed_introspection.py F'
E           and: u''
E           and: u'=================================== FAILURES ==================================='
E           and: u'_____________________________________ test _____________________________________'
E           and: u''
E           and: u'mocker = <pytest_mock.MockFixture object at 0x7f4646277c90>'
E           and: u''
E           and: u'    def test(mocker):'
E           and: u'        m = mocker.Mock()'
E           and: u"        m('fo')"
E           and: u">       m.assert_called_once_with('', bar=4)"
E       fnmatch: "*AssertionError: Expected call: mock('', bar=4)*"
E          with: u"E       AssertionError: Expected call: mock('', bar=4)"
E       fnmatch: "*Actual call: mock('fo')*"
E          with: u"E       Actual call: mock('fo')"
E       nomatch: '*pytest introspection follows:*'
E           and: u'E       '
E       fnmatch: '*pytest introspection follows:*'
E          with: u'E       pytest introspection follows:'
E       nomatch: '*Args:'
E           and: u'E       '
E       fnmatch: '*Args:'
E          with: u'E       Args:'
E       nomatch: "*assert ('fo',) == ('',)"
E           and: u'E       '
E           and: u'E       Kwargs:'
E           and: u''
E           and: u'test_detailed_introspection.py:4: AssertionError'
E           and: u'=========================== 1 failed in 0.01 seconds ==========================='
E           and: u''
E       remains unmatched: "*assert ('fo',) == ('',)"

/tmp/autopkgtest.31sHEi/autopkgtest_tmp/test_pytest_mock.py:535: Failed
----------------------------- Captured stdout call -----------------------------
============================= test session starts ==============================
platform linux2 -- Python 2.7.14+, pytest-3.2.5, py-1.4.34, pluggy-0.4.0
rootdir: /tmp/pytest-of-root/pytest-0/testdir/test_detailed_introspection0, inifile:
plugins: mock-1.6.3
collected 1 item

test_detailed_introspection.py F

=================================== FAILURES ===================================
_____________________________________ test _____________________________________

mocker = <pytest_mock.MockFixture object at 0x7f4646277c90>

    def test(mocker):
        m = mocker.Mock()
        m('fo')
>       m.assert_called_once_with('', bar=4)
E       AssertionError: Expected call: mock('', bar=4)
E       Actual call: mock('fo')
E       
E       pytest introspection follows:
E       
E       Args:
E       
E       Kwargs:

test_detailed_introspection.py:4: AssertionError
=========================== 1 failed in 0.01 seconds ===========================
========== 3 failed, 38 passed, 1 skipped, 2 xfailed in 0.69 seconds ===========

The plugin seems to be correctly registered:

============================= test session starts ==============================
platform linux2 -- Python 2.7.14+, pytest-3.2.5, py-1.4.34, pluggy-0.4.0 -- /usr/bin/python2.7
cachedir: .cache
rootdir: /tmp/autopkgtest.31sHEi/autopkgtest_tmp, inifile:
plugins: mock-1.6.3
collecting ... collected 44 items

The same thing happens for both Python 2 and Python 3.

@nicoddemus
Copy link
Member

Hi @ghisvail,

Those tests expect pytest's assertion rewrite to be enabled to work, and indeed I can reproduce the problem if I execute the tests with --assert=plain.

Pytest assertion rewrite is enabled by default, can you check your command-line or PYTEST_ environment variables for that setting?

Either way I think the proper solution is to skip those tests when executed with assert=plain.

@nicoddemus
Copy link
Member

The tests which require assertion rewrite are now skipped with --assert=plain.

This was referenced Feb 16, 2018
This was referenced Mar 1, 2018
@ghisvail
Copy link
Contributor Author

ghisvail commented Mar 5, 2018

Either way I think the proper solution is to skip those tests when executed with assert=plain.

Still not working, unfortunately.

@ghisvail
Copy link
Contributor Author

ghisvail commented Mar 5, 2018

Works if I call pytest with --assert=plain explicitly

@nicoddemus
Copy link
Member

Still not working, unfortunately.

Yes, after some more investigation I believe the reason is explained in #102 (comment).

@nicoddemus
Copy link
Member

@ghisvail should we close this? As I said, I believe the reason might be related to how Debian packages the modules (#102 (comment)).

milafrerichs added a commit to technologiestiftung/OpenData-Harvester that referenced this issue Jun 20, 2018
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