Suite Quality and Speed
19. What does code coverage tell you, and what does it not?
Coverage tells you which lines or branches ran during the tests. It does not tell you whether the tests checked anything: a test with no assertions can reach full coverage.
It is most useful in reverse, to find important code that no test runs. Branch coverage is stricter than line coverage because it counts both sides of each condition.
A hard 100% target tends to produce low-value tests of getters and generated code.
20. What is mutation testing?
Mutation testing measures whether the tests catch bugs. The tool makes small changes to the code, such as turning > into >= or removing a call, and reruns the tests. A mutant that no test catches shows a gap that coverage would miss.
PIT (Java), Stryker (JavaScript, C#, Scala) and mutmut (Python) are common. It is slow, so teams run it on critical modules or on changed code only.
21. What causes flaky tests, and how do you fix them?
Most flaky tests come from a few causes: real time and sleeps, shared state between tests, test order, async code that is not awaited, and network calls.
- Replace
sleep with fake clocks or with waiting for a condition.
- Give each test its own data and reset shared state.
- Await every promise or future the test starts.
- Stub external services at the HTTP boundary.
A flaky test found in CI should be fixed or quarantined within days. Retrying it automatically hides a real bug about as often as it hides a test bug.
22. How do you speed up a slow test suite?
Measure first: most suites have a few slow tests that dominate. Then run tests in parallel (pytest-xdist, JUnit's parallel execution setting, Vitest's worker pool), which requires the isolation from question 18.
Push checks down the pyramid, share expensive setup such as one database container per run, and in CI run only the tests affected by a change before the full suite. Removing sleeps is often the biggest single win.
23. How do you add tests to legacy code that has none?
Start with characterization tests: tests that record what the code does today, right or wrong, so a refactor cannot change behavior unnoticed.
Michael Feathers' approach is to find a seam, a place to swap a dependency without editing much code, and test through it.
Test at a higher level first, often through the API, because the internals are not yet testable. Break dependencies in small, safe steps, then add unit tests as the code gets smaller units.
24. How do you test asynchronous code without false passes?
Make the test wait for the async work, and assert on its result. A test that starts a promise and returns early passes before the assertion runs.
test("loads the user", async () => {
await expect(loadUser(1)).resolves.toMatchObject({ id: 1 });
});
test("rejects unknown users", async () => {
await expect(loadUser(999)).rejects.toThrow("not found");
});
Forgetting await before expect(...).resolves is the common bug. In pytest, async tests need a plugin such as pytest-asyncio or AnyIO's pytest plugin.
25. Should you test private methods?
No, test them through the public methods that use them. A private method is an implementation detail, and tests that reach into it break whenever it changes.
If a private method feels like it needs its own tests, it usually holds logic that belongs in its own class with a public API. Extracting it makes the tests straightforward and the design clearer.