Wrap-up and resources
Key Takeaways
- 1. Unit tests: A unit test checks one function in isolation, quickly, with a stable result. Write a test for each behaviour and for every bug you fix.
- 2. Integration tests: A unit test proves the piece is sound; an integration test proves the pieces speak the same language. Make every test start with a clean database.
- 3. Clean code and refactoring: Code is read far more than it is written. Name things for what they do, make each function do one thing, and never refactor without tests.
Glossary
| Term | Definition | Part |
|---|---|---|
| AAA | Arrange, act, assert: the shape of every test. | 1 |
| parametrize | Running the same test over a table of cases. | 1 |
| Mock | A stand-in that records calls in place of the real dependency. | 1 |
| Integration test | Runs several real parts together. | 2 |
| Test pyramid | Many unit, fewer integration, few end-to-end. | 2 |
| Fixture | Shared setup recreated for each test. | 2 |
| Refactoring | Changing code's shape without changing its behaviour. | 3 |
| Early return | Leaving the function on special cases first to cut nesting. | 3 |
| Magic number | An unnamed number in code, replaced by a named constant. | 3 |
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Official docs5
pytest: Get startedYour first test, running, parametrize and fixtures.docs.pytest.orgPython docs: unittest.mockMocks and checking calls.docs.python.orgFastAPI: TestingTestClient and overriding dependencies.fastapi.tiangolo.comTestcontainersReal databases in throwaway containers for tests.testcontainers.comRefactoring catalog (Martin Fowler)The list of refactorings with their names and steps.refactoring.com
Done with this lesson?
Sign in to save your progress.