Why is real network IO in unit tests bad?

⚓ rust    📅 2025-07-04    👤 surdeus    👁️ 3      

surdeus

I've always taken the stance that this was bad, at that unit tests should be scoped to only functions which do no IO, or maybe only some filesystem IO depending on the test.

However, thinking more about the cargo testing ecosystem, cargo test doesn't differentiate between running unit tests and integ tests - it runs all tests at once. So if it doesn't care, then why should I?

I'd think whether I care if a test (unit or otherwise) does real network IO only depends on the environment of the test (Does it have a network connection / creds? Could it mess up my laptop's filesystem?). I.e. if I'm fine with an integ test making network calls, why shouldn't I be fine with a unit test making network calls in the same environment?

I get mocking is useful for testing control flow, like what happens if a network request fails, so I still see the use case for that.

Is the general aversion to network IO in unit tests only because it prevents testing the all results of the IO call (e.g. network errors) / conditional logic?

1 post - 1 participant

Read full topic

🏷️ rust_feed