top of page

Why Does a Python Test Still Call the Real Dependency After You Mock It?

Oct 26
3 min read

A test is intended to replace an external dependency with a harmless local stand-in. Yet the test still reaches the real implementation. The replacement may be valid but attached to a different name from the one the application actually uses.


This is a practical problem for contributors testing integrations. A mocked dependency should be demonstrably inside the exercised path before a test is allowed to rely on it for isolation.


Trace the name at the call site


Python's mock documentation explains that patching must target the name looked up by the system under test, which may differ from the place where the object was originally defined.


Read the import and the call together. Do not choose the patch target from the dependency's documentation alone. The application's import style determines which reference the tested code uses.


Keep the first diagnostic local. Use a dependency that returns a distinctive test value or records an invocation. There is no need to involve a real messaging, payment or customer system to establish whether the replacement is reached.


Prove the replacement participates in the test


Illustrative imported helper


Suppose a routing module imports a helper into its own namespace and later calls that local name. Replacing only the original helper's definition can leave the routing module holding its earlier reference.


The test should target the reference used by the routing module. Confirm this by arranging a clearly recognizable return value and asserting that the result incorporates it. A test that passes without ever using the replacement may be exercising the wrong branch.


The exact patch path is project-specific. Derive it from the code you are reviewing rather than copying a path from an unrelated example.


Illustrative alternate import style


Another module may import a dependency module and access a helper through that module. Its lookup path differs from the first example. The same-looking business operation can therefore require a different replacement target.


If a refactor changes the import style, review the tests' replacement points as part of the change. A test may continue to run while no longer controlling the boundary its author intended.


Assert the business result as well as the interaction


Checking that a stand-in was called is useful, but it does not prove the returned business result is correct. Assert the important output or state transition and the required interaction separately.


For an illustrative routing operation, verify the chosen destination and the request data passed to the dependency. Include a failure response to check how the workflow handles an unavailable dependency. Keep those expectations tied to the intended contract.


Avoid reproducing every internal implementation detail in the test. A test tightly coupled to incidental helper calls can become noisy during harmless refactoring while still missing a wrong user-visible result.


Keep isolation evidence separate from integration evidence


A successful local replacement proves something about the code path under the configured test conditions. It does not prove that the real external service accepts the request or that credentials and permissions are correct.


Use separate, appropriately controlled integration checks for that boundary. The local test should remain fast and safe enough to run without depending on a live third-party operation.


Document what was replaced and what remained real. This lets a reviewer understand the evidence instead of assuming that all passing tests exercise the same environment.


The demo side-effect guide explains why environment boundaries matter beyond testing. Begin by tracing the name used at the call site, then demonstrate that the replacement is reached. A mock is a controlled test input only when the tested code actually uses it.


Editorial note


Prepared with AI assistance. Primary sources and the FutureOS article inventory were checked on October 26, 2026. Examples are illustrative, not customer results. FutureOS is described as being in prelaunch in its current introduction. This guide does not claim an available product feature, integration or service, and no independent expert review is claimed.


Comments


Commenting on this post isn't available anymore. Contact the site owner for more info.
bottom of page