Back to articles
I Got Tired of Mocking APIs in pytest. So I Built a Cleaner Way.

I Got Tired of Mocking APIs in pytest. So I Built a Cleaner Way.

via Dev.toSherin Joseph Roy

If you’ve written integration tests in Python long enough, you’ve hit this wall. Your test calls three external services. You mock one endpoint. Then another. Then another. Suddenly your test is 60 lines long and half of it is patching. At that point you’re not testing behavior. You’re maintaining scaffolding. I ran into this repeatedly while working on service-to-service flows, especially when: A single operation triggered multiple HTTP calls Different tests required different combinations of responses Fixtures started turning into mini frameworks The tooling ecosystem is strong. responses , unittest.mock , httpx mocking utilities. But once endpoint count increases, ergonomics start degrading. The issue is not capability. It is readability. The Breaking Point Here is what multi-endpoint mocking often turns into: import responses @responses.activate def test_checkout (): responses . add ( responses . GET , " https://api.example.com/users/1 " , json = { " id " : 1 , " name " : " Alice "

Continue reading on Dev.to

Opens in a new tab

Read Full Article
1 views

Related Articles