I want to make my tests "real" unittests in a strict meaning. There shouldn't be any dependencies. All dependencies should be mocked.
The class Bar in module bar need to be tested. But in some situations it will have a member of type Foo from module foo. The goal is that the unittest does not have to import foo or foo.Foo.
My mocking seems to work. But I'm not sure how to test if Bar() now does have tried to instantiate foo.Foo(). My assert_called() in the below example failed with Expected 'mock' to have been called.. And that assert also doesn't give me all information's I want.
This is the test code:
#!/usr/bin/env python3 import unittest from unittest import mock import bar # The goal ist to test "Bar" wihtout importing "foo.Foo" as an extra dependency class MyTest(unittest.TestCase): def test_bar(self): mock_foo = mock.Mock() bar.foo = mock_foo b = bar.Bar(7) print('') print(f'{mock_foo=}') print(f'{b.y=}') # Of course this doesn't work, because "foo" is not imported # self.assertIsInstance(b.y, foo.Foo) print(mock_foo.__dict__) mock_foo.assert_called() # failes def test_int(self): b = bar.Bar(8) self.assertIsInstance(b.y, int) if __name__ == '__main__': unittest.main() Here comes module bar:
import foo class Bar: def __init__(self, y): if y == 7: self.y = foo.Foo(7) else: self.y = y And this is module foo:
class Foo: def __init__(self, x): self.x = x
Bar.__init__can take an optional argument (defaulting tofoo.Foo) to call when necessary. Then to test, you need only make a call likeb = bar.Bar(7, mock_foo).