Module 20 · Building Real Python Applications — topic 10 of 13
Application testing
Testing a whole application: unit tests for the service layer, testing command-line output, testing persistence with temporary folders, injecting fake clocks and collaborators, and deciding what to test at each level.
Learning objectives
- Unit-test the service layer
- Test command-line output and persistence
- Use fakes and injected dependencies to make tests repeatable
Definition
Application testing combines unit tests of individual classes and functions with tests of larger pieces — the command loop, storage, the full flow — using temporary files and fake collaborators so tests are fast and repeatable.
Introduction
Unit tests on small functions are a start. A real application also needs confidence that commands print the right thing and data really is saved.
Why it matters
Testing at several levels catches both small logic bugs and integration problems between components.
Detailed explanation
Unit tests check logic classes and functions with no files or input involved — they are fast and should be the majority.
Integration tests check components together: the service with real file storage in a temporary folder.
End-to-end tests drive the application like a user — feeding commands and checking output.
Capture printed output with contextlib.redirect_stdout (or pytest's capsys) and compare it with the expected lines.
Use tempfile.TemporaryDirectory (or pytest's tmp_path) so tests never touch real data and clean up after themselves.
Inject things that vary — the current date, random numbers, network calls — so tests can supply fixed values.
A smoke test runs the main flow once to confirm nothing is fundamentally broken; run it before every demo.
Write a test for every bug you fix, at the lowest level that reproduces it.
Syntax
- Mostly unit tests.
- Capture output for CLI tests.
- Temporary folders and fakes.
Code examples
Testing the service layer
2 True
Testing command-line output
CLI output test passed
More examples
Persistence with a fake clock
persistence test passed with a fake clock
Step-by-step explanation
- Diary takes today as a parameter, defaulting to the real date.today.
- The test supplies fixed dates, so its expected output never changes.
- A temporary directory keeps the test away from real files and is deleted automatically.
Expected output
Running “Persistence with a fake clock” prints exactly:
Practical applications
- CI pipelines running unit, integration and end-to-end tests.
- Testing time-dependent features such as reminders and due dates.
Best practices
- Write mostly fast unit tests.
- Use temporary folders for storage tests.
- Inject clocks and other changing inputs.
Common mistakes
Tests that use real data files
Avoid
Prefer
Tests could overwrite real data and depend on its contents.
Tests depending on today's date
Avoid
Prefer
Tests must give the same result every day.
Important notes
- pytest's tmp_path, capsys and monkeypatch fixtures make all of these techniques shorter.
Key takeaways
- Test logic, components and the full flow.
- Capture output and use temporary folders.
- Inject fakes for repeatability.
Try it yourself
- 1Write an end-to-end test that adds three expenses through the command loop and checks the report output.
- 2Inject a fake clock into a reminder feature and test that overdue items are detected.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
8 questions cover the “Building Real Python Applications” module. Answers and explanations are revealed after you submit.
- AdvancedWhat does MVP stand for when planning an application?
- AdvancedWhere should input validation ideally happen?
- AdvancedWhat is printed?