Part 5 of 7 · 2 min read
Habits, and working like a professional
What keeps a test suite useful over years, what teams add, and how this comes up in interviews.
- Fast. The whole suite should run in seconds, so that you run it constantly.
- Independent. Tests must pass in any order and on their own. No shared global state.
- Deterministic. Fake the clock and randomness. A test that sometimes fails for no reason (a flaky test) teaches people to ignore failures.
- Readable. One reason to fail per test, and a clear name.
- Coverage is a flashlight, not a goal. It shows which lines never ran. It does not show that they are correct, so do not chase 100%.
- Run them in CI on every push (module 7), so that a red test blocks a merge.
What you will use daily at work
| Practice | What it looks like |
|---|---|
| Tests run before every merge | CI runs pytest on each pull request, and merging is blocked while it is red. |
| A test with every bug fix | Write the failing test first, see it fail, fix the bug, see it pass. |
| Coverage reports as a guide | pytest --cov (with the pytest-cov plugin) shows untested lines. Teams use it to find gaps, not to set a score. |
| Test markers | @pytest.mark.slow or integration, so quick tests run on every save and slow ones run in CI. |
| Async tests | The pytest-asyncio plugin lets you write async def test_... directly (module 4). |
| Property-based tests | The hypothesis library generates many odd inputs for you and finds edge cases you did not think of. |
| Snapshot or golden tests | For agents: record a good run and compare later runs to it. Level 9 builds on this. |
Draft: to be checked against the current pytest and plugin documentation before this page goes live.
Interview angle
Answer in your head first, then open it.
How do you test code that calls an LLM?
Inject the client, replace it with a fake that returns scripted replies or errors, test everything around it deterministically, and keep a separate small evaluation suite for the model's real behaviour.
What is a fixture?
A reusable piece of set-up (and tear-down) that pytest injects into tests by parameter name.
Mock versus fake?
A fake is a lightweight working substitute; a mock also verifies how it was called.
What would you test first in a new service?
Validation, then each unit of logic, then the failure paths of the endpoints.
What is a flaky test and why is it harmful?
A test that sometimes passes and sometimes fails with no code change. It teaches people to ignore red results.