[issue21478] mock calls don't propagate to parent (autospec)

Karthikeyan Singaravelan report at bugs.python.org
Mon Oct 14 13:40:46 EDT 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

I tried your example as below using __name__. I received an AttributeError for which issue38473 in 3.7.5RC1 and 3.8.0RC1 and opened issue38473 and I am running my below code under that issue PR. For 3.7.4, I received manager.mock_calls to be an empty list since it doesn't contain the patch for this issue. Can you please confirm my results too.

➜  cpython git:(bpo38473) cat ../backups/bpo21478.py
import unittest
from unittest import TestCase
from unittest.mock import patch, Mock, call, ANY


class MyObject:
    def __init__(self):
        self.foo = 0
        self.bar = 0

    def set_foo(self, value):
        self.foo = value

    def set_bar(self, value):
        self.bar = value


def do_something():
    o = MyObject()
    o.set_foo(3)
    o.set_bar(4)
    return "something unrelated"


class MyObjectTest(TestCase):
    @patch(f"{__name__}.MyObject.set_bar", autospec=True)
    @patch(f"{__name__}.MyObject.set_foo", autospec=True)
    def test_do_something(self, mock_set_foo, mock_set_bar):
        manager = Mock()
        manager.attach_mock(mock_set_foo, "set_foo_func")
        manager.attach_mock(mock_set_bar, "set_bar_func")
        do_something()
        assert manager.mock_calls == [
            call.set_foo_func(ANY, 3),
            call.set_bar_func(ANY, 4),
        ]
        manager.assert_has_calls([call.set_foo_func(ANY, 3), call.set_bar_func(ANY, 4)])


if __name__ == "__main__":
    unittest.main()


➜  cpython git:(bpo38473) ./python ../backups/bpo21478.py
.
----------------------------------------------------------------------
Ran 1 test in 0.006s

OK

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue21478>
_______________________________________


More information about the Python-bugs-list mailing list