A unit test checks one small piece of code — like a single method — in isolation. Fast and precise. An integration test checks that multiple pieces work together, like a database plus a service plus a controller.
Property‑based tests are the cool ones: instead of checking one specific input, you describe properties that should always hold ("reversing a list twice gives back the original"). The tool generates hundreds of random inputs to try to break it.
Sometimes you can't use the real thing in a test — maybe it's slow (a database) or unpredictable (a weather API). That's when you use a test double, a stand‑in object:
Stub — returns a canned answer. Mock — verifies that specific calls were made. Fake — a simpler working version (like an in‑memory database). Spy — records calls so you can inspect them after the test.
TDD flips the usual order: you write the test before the code. The cycle has three steps:
The result: code that's exactly as complex as needed, backed by tests that prove it works. It feels slow at first but saves hours of debugging later.
Refactoring means improving code structure without changing its behaviour. It's a bit like tidying your room: nothing new arrives, but now you can find everything. Common moves include:
Extract Class — split one big class into two focused ones. Inline — merge a tiny class back into its caller. Move Method — relocate a method to the class that uses it most. Rename — make unclear names obvious.
Static analysis tools scan your code without running it, looking for bugs, bad patterns, and risky code. Linters check style and formatting (e.g., flake8, ESLint). Code metrics measure things like complexity, duplication, and test coverage.
You'll learn to read the warnings — not ignore them — and set thresholds so your code can't slowly rot into a mess.
Continuous Integration (CI) runs your tests automatically every time you push code. If something breaks, you know within minutes. No more "it worked on my laptop."
Popular services like GitHub Actions or GitLab CI can also run linters, static analysis, and deployment steps. Combined with code review, CI is the safety net that catches bugs before they reach users.
First unit test: Write a Calculator class with add() and divide(). Write tests that cover happy paths (2 + 2 = 4) and edge cases (divide by zero). Run them.
Stub a slow service: Create a WeatherService stub that always returns 22 °C. Use it in a test to verify that your OutfitRecommender suggests t‑shirt weather.
TDD kata: Using TDD, build a FizzBuzz function. Write the test first (fails), implement minimum code to pass, then refactor. Do this for each rule.
Extract class: Take a User class that handles account data, email formatting, and database saving. Refactor by extracting UserRepository and EmailFormatter classes.
Lint run: Install a linter for your language (flake8 for Python, ESLint for JavaScript). Run it on a recent project. Fix the top five warnings.
CI setup: Push a small project to GitHub and add a GitHub Actions workflow that runs tests on every push. Break a test on purpose and watch CI catch it.
By the end of this module, you'll be able to: write unit and integration tests, use mocks and stubs, follow the TDD cycle, apply common refactorings, run linters and static analysis, and set up CI to keep your projects healthy.