What if your tests never broke because of slow or missing external services?
Why Testing with external services in PyTest? - Purpose & Use Cases
Imagine you have a website that talks to a weather service to show the current temperature. Every time you want to check if your site works, you open the site and wait for the real weather service to respond.
This manual way is slow because you wait for the real service every time. Sometimes the service is down or slow, making your testing frustrating. Also, you can't control the weather data, so you can't test special cases easily.
Testing with external services lets you replace the real service with a fake one during tests. This fake service gives quick, predictable answers so your tests run fast and always the same way. You can test all situations without depending on the real service.
import requests def test_weather(): response = requests.get('https://real-weather.com/api') assert response.status_code == 200
import requests def test_weather(monkeypatch): def fake_get(url): class FakeResponse: status_code = 200 return FakeResponse() monkeypatch.setattr(requests, 'get', fake_get) response = requests.get('https://real-weather.com/api') assert response.status_code == 200
It enables fast, reliable, and repeatable tests that work even when the real external service is unavailable or slow.
A developer testing a payment system can fake the bank's response to check how the app handles success, failure, or timeout without calling the real bank every time.
Manual testing with real services is slow and unreliable.
Faking external services makes tests fast and predictable.
This approach helps test all scenarios safely and repeatedly.