Learn how to build, test, deploy, and maintain robust software systems.
Software testing is how you gain confidence that code behaves correctly. A unit test verifies one small, isolated piece of behavior in isolation. Unlike integration or end-to-end tests, a unit test does not hit a real database or network — it checks a single function or class against expectations. This lesson covers how to structure a unit test, what a test runner and assertions are, when and how to mock dependencies, edge cases, Test-Driven Development, and what code coverage actually means. The goal is not to chase a percentage but to write meaningful tests that catch real bugs and let you refactor without fear.
Tests exist at different levels. Unit tests check one piece in isolation; integration tests check pieces working together; end-to-end tests exercise the whole app. This lesson focuses on unit tests.
A unit test gives a known input to a single unit (a function or method) and checks the output. It is isolated — no real database or network.
A test runner discovers and runs your tests and reports results (Jest, PyTest, and others). Assertions are the checks that compare actual output to expected output.
| Double | What it is |
|---|---|
| Stub | Returns fixed values |
| Mock | Records interactions and can assert they happened |
| Spy | Wraps a real object to observe calls |
| Fake | A simplified working implementation |
When a unit depends on something slow or external (an API, a database), you mock that dependency so the unit test stays fast and isolated.
If a class accepts its dependencies as parameters instead of creating them internally, you can pass a fake in a test. This is a big reason dependency injection is popular.
TDD writes the test first, then the code. It is a useful discipline, not a universal mandate.
Coverage measures how much of the code is executed by tests (lines and branches). High coverage does not mean the tests are meaningful — a test can run code without checking anything important.
Each test asserts one behavior. The function is a pure, isolated unit.
Verify the zero-padding behavior for single-digit days.
(passes)
Verify a function raises when given bad input.
(passes)