Part 2: Testing¶
Note
Part 2 of 4. This part uses the cookiecutter-git-workshop-testing repository for its hands-on tasks. It assumes you’ve completed Part 1: Git Basics — you’ll use the same branch → commit → push → merge request cycle for every sub-task below.
See also
A condensed slide deck of this part is available for presenting: Part 2 slides (see Presentation Slides).
Why bother testing?¶
There are always reasons ready at hand to skip writing tests: the project is late, “the code works, don’t touch it”, “my code is too complicated to test”, or “I’m the only one working on this anyway”. All of these are understandable in the moment, and all of them get more expensive to ignore the longer a project lives. Untested code isn’t free of bugs — it’s just free of evidence about whether it has bugs, which means every future change to it is a gamble.
Writing tests upfront feels like it slows you down, and locally, for a five-minute script, it often does. But it’s an investment that pays off as soon as a project has more than one moving part or more than one day of lifespan: tested code gives you certainty about how it behaves, documents its expected usage far more reliably than a comment (comments rot; a failing test tells you immediately when reality and documentation disagree), and lets you make changes with confidence instead of dread. Put simply: let the computer double-check your work instead of doing it by hand every time.
What is a unit test?¶
A unit test checks that one specific, small piece of your code (the “unit” — usually a single function) does what you expect, by feeding it known input and asserting on the output:
def test_addition():
# given
summands = [3, 2]
# when
the_sum = sum(summands)
# then
assert the_sum == 5
Notice the three-part shape (often labelled “given / when / then” or
“arrange / act / assert”): set up some input data, run the code under test,
then assert on the result. Every unit test you write in this workshop will
have this same shape. The assertion is what actually makes it a test —
without assert the_sum == 5, the code above would just quietly run and
tell you nothing.
A few habits keep unit tests useful rather than a false sense of security:
Do |
Avoid |
|---|---|
bring your own test data (construct it inline, in the test) |
reading test data from external files |
use temporary files/dirs if you must touch disk |
making real network/API calls from a test |
include a “negative control” (an input that should not pass) |
relying on mocks to fake out your own code |
keep tests self-sufficient (no dependency on test order) |
|
test your own package’s logic, not third-party libraries |
The “avoid mocks” stance is a deliberate simplification for this workshop: mocking (replacing a real dependency with a fake stand-in) is a real and sometimes necessary technique, but it’s easy to overuse in a way that tests your mocks instead of your code. Learning to write tests against real, small, self-contained inputs first will serve you better than reaching for mocking early.
Refactoring: changing code without changing behavior¶
Refactoring means rearranging or rewriting code — improving its structure, readability, or performance — without changing what it actually does from the outside. This is exactly where tests earn their keep: if you have a test suite that passes before your refactor and still passes after, you have real evidence that you didn’t break anything, rather than just a feeling. Without tests, “harmless cleanup” is a leap of faith every time.
Continuous integration (CI)¶
Continuous integration automatically runs your checks — tests, and often a linter — every time code is pushed, on a clean server rather than just on your own machine. Think of it as a traffic light for your repository: green means everything that was checked actually passed, on a fresh checkout, not just “it worked when I ran it locally five minutes ago” (which can hide problems like an accidentally-uncommitted file, or a dependency you happened to already have installed).
For CI to actually protect a project, two social rules matter as much as the technical setup:
the main branch is protected — nobody pushes to it directly
the only way to change the main branch is through a merge request, and you only merge once the checks are green
For today’s workshop, CI is already configured for you as long as your repository lives in the right GitLab group. In your own future projects, GitLab’s CI introduction guide is the place to start.
Test-driven development (TDD)¶
TDD flips the usual order: instead of writing code and then a test for it, you write a failing test first, describing the behavior you want, and only then write the minimum code needed to make it pass. Taken strictly, the rules are: write a failing test; write just enough code to make it pass; refactor if needed; repeat for the next bit of behavior.
Why bother with the extra discipline? Writing the test first forces you to think about the interface before the implementation — what do I actually need to call, with what arguments, to get what result? — which tends to produce cleaner APIs than designing bottom-up. It also keeps you working in small, verifiable steps, so problems get caught immediately instead of accumulating across a large chunk of untested code.
TDD isn’t free, though: it works best on new (“greenfield”) code where the desired behavior is reasonably clear upfront, and it can feel counterintuitive at first, since you’re deliberately writing code that fails before writing the code that fixes it. Task 3 below has you try it on a small, real feature so you can judge for yourself.