testing-cabal / mock

The Python mock library

Home Page:https://docs.python.org/dev/library/unittest.mock.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

opened this issue · comments

What versions are you using?

  • Python: 3.9.0
  • Mock: 4.0.3
  • Operating System: Linux

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 f7e3ea8.
The test code (as in c3153c8) 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).

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.

@len-chambley, you're absolutely correct. See #497.

@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.

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