testing-cabal / mock

The Python mock library
https://docs.python.org/dev/library/unittest.mock.html
BSD 2-Clause "Simplified" License
536 stars 107 forks source link

Test test_bool_not_called_when_passing_spec_arg() fails (mixup unittest.mock vs mock?) #496

Closed ghost closed 3 years ago

ghost commented 3 years ago

What versions are you using?

What happened?

Mock fails its test suite, more precisely the test test_bool_not_called_when_passing_spec_arg() with AssertionError: 1 != 0.

I believe that the culprit is a not completely backported/adapted patch, namely f7e3ea85b4f99604e8e05f4659825f69f3f29bc6. The test code (as in c3153c8f572b689b951482f599fa5faded012dc7) is

    def test_bool_not_called_when_passing_spec_arg(self):
        class Something:
            def __init__(self):
                self.obj_with_bool_func = unittest.mock.MagicMock()

        obj = Something()
        with unittest.mock.patch.object(obj, 'obj_with_bool_func', autospec=True): pass

        self.assertEqual(obj.obj_with_bool_func.__bool__.call_count, 0)

which solely tests unittest.mock which is the builtin Python implementation and not the implementation provided by this package (which would be mock without leading unittest.). Replacing unittest.mock with mock lets the test pass.

If the above analysis is correct, than maybe also the tests test_isinstance_under_settrace() and test_all() need to replace unittest.mock with mock though these tests pass on my system with and without modifications (though I have not tested with other Python versions).

It is important to note that a Python version which does not have the fix for bpo-42532 must be used to reproduce (e.g. Python 3.9.0).

tirkarthi commented 3 years ago

It's interesting why CI didn't catch this during backport tests or probably it had a fixed version of Python. As per the report I guess it's an issue with tests using unittest.mock and not the actual functionality.

cjw296 commented 3 years ago

@len-chambley, you're absolutely correct. See https://github.com/testing-cabal/mock/pull/497.

cjw296 commented 3 years ago

@tirkarthi - we weren't testing against 3.9 and there were a couple of places where we were using unittest.mock when we should have been using mock.mock. I believe the PR fixes both problems.

bnavigator commented 2 years ago

It's also happening with Python 3.10.1, #497 fixes it.