[issue41768] unittest.mock spec calls class properties

Terry J. Reedy report at bugs.python.org
Fri Sep 18 17:50:35 EDT 2020


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Without lines numbers, I cannot test which of the two identical asserts failed. Either comments or msg arguments will differentiate.  Nor can I run snippets from two different files. Here is a minimal reproducible self-contained code that demonstrates the claim (which I verified on 3.9 and current master).

import unittest
from unittest.mock import Mock

class SomethingElse(object):
    def __init__(self):
        self._instance = None

    @property
    def instance(self):
        if not self._instance:
            self._instance = 'object'

class Test(unittest.TestCase):

    def test_property_not_called_with_spec_mock(self):
        obj = SomethingElse()
        self.assertIsNone(obj._instance, msg='before') # before
        mock = Mock(spec=obj)
        self.assertIsNone(obj._instance, msg='after') # after

unittest.main()

----------
nosy: +michael.foord, terry.reedy
versions: +Python 3.10, Python 3.9

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


More information about the Python-bugs-list mailing list