Message 377140 - Python tracker (original) (raw)

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()