Content · Programming · Python

Python Keep Unit Tests Independent of Network Access

Network-dependent tests can fail because of DNS, rate limits, outages or slow responses rather than application logic.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Network-dependent tests can fail because of DNS, rate limits, outages or slow responses rather than application logic.

Understanding the Concept


Network-dependent tests can fail because of DNS, rate limits, outages or slow responses rather than application logic. Inject a transport or mock it so the core behavior can be checked offline.

Add a separate integration test against a dedicated test server when the real protocol also needs verification.

How this fits into a real test suite


A reliable test starts with a specific expected behavior. Keep the fixture small, arrange the input, call the real code, and assert a result the reader can verify. Add one boundary or failure case so the test does not pass only for a convenient example. In a larger project, run the same test automatically after each change.

Tests are repeatable experiments. Use temporary resources and injected dependencies where possible; when the environment is part of the requirement, add a separate integration test rather than weakening the unit test.

Worked Example


Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.

from unittest.mock import Mock

def get_status(client):
    response = client.get("/health")
    return response.status_code == 200

client = Mock()
client.get.return_value.status_code = 200
print(get_status(client))
client.get.assert_called_once_with("/health")

Walkthrough: What Each Part Does


  • The imports make unittest.mock available. These are standard-library building blocks unless the example explicitly names an external tool.
  • The definition get_status creates reusable behavior. Its body runs when called, awaited or instantiated; defining it alone does not run the body of an ordinary function.
  • The coordination call waits for a result or a completion condition. Predict what would happen if the worker failed or never became ready.
  • The final check compares actual behavior with an explicit expectation. A successful test tells you that this particular case passed, not that every possible input works.

This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.

Example Output


True

The output above was captured by running the example in the available Python environment. The order of concurrent events, durations and platform-dependent values can legitimately differ on your computer.

Important Behavior and Edge Cases


Mocking the very calculation being tested instead of only the external boundary.

Add a separate integration test against a dedicated test server when the real protocol also needs verification.

Practice Exercise


Test success and failure responses without sending an HTTP request.

Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.

Further reading: official documentation and API reference.

Common Beginner Mistakes


  • Mocking the very calculation being tested instead of only the external boundary.
  • Writing assertions that check implementation details but fail to validate the behavior that matters to the caller.

Key Takeaways


  • Network-dependent tests can fail because of DNS, rate limits, outages or slow responses rather than application logic.
  • Add a separate integration test against a dedicated test server when the real protocol also needs verification.
  • Practice: Test success and failure responses without sending an HTTP request.
Share this resourceSend this page to someone who may find it useful.