There are three files, a file with a test and two simple modules: a.py: import b def bar(): b.foo() b.py: def foo(): print("Hello from b.py") test_a.py: from a import bar from unittest import mock def test_bar(): with mock.patch('b.foo') as mock_foo: bar() mock_foo.assert_called_once() The goal is to mock the function foo from the module b which is then called inside the function bar. The above code works as expected and the test will pass. However, when the file a.py will be modified as follows, where the foo function is imported directly from the module b, then the mocking will fail and the original, non-mocked foo function is getting called inside the function bar: from b import foo def bar(): foo() Why the function foo is not mocked properly in the test when it's imported with the from keyword in the a module? Continue reading...