Software Quality
Software Quality

The Craft of Writing Tests

Mustafa MašetićMay 25, 20262 min read4 reads1 shares
Share
Testing is a design activity as much as a verification one. The tests you write reveal how well you understand your own system.

Tests Are a Design Tool

Most engineers treat testing as something that happens after the code is written. You build the feature, then you write tests to confirm it works. But this ordering misses something important. The act of writing a test forces you to think about your code from the outside — as a caller, not an author. When testing feels hard, it's usually because the code is hard to use. The test is the first user of your API, and if it's awkward to set up, awkward to call, and awkward to assert against, that's not a testing problem. That's a design problem the test just revealed.

Naming Is Everything

The name of a test is its specification. A test called "test_user_login" tells you almost nothing. A test called "redirects_to_dashboard_after_successful_login_with_valid_credentials" tells you exactly what behaviour is being verified, under what conditions, and what the expected outcome is. When that test fails six months from now — and it will — the name alone should tell the next engineer what broke without them needing to read the implementation. Write test names as if you're writing documentation for someone who has no context and is under pressure.

The Maintenance Problem

The biggest hidden cost in test automation is maintenance. Tests that are tightly coupled to implementation details — checking internal state, relying on specific class names, asserting on exact pixel positions — become a drag on every refactor. Every time the code changes for a legitimate reason, a dozen tests break for no good reason. The solution is to test behaviour, not implementation. Ask yourself: if I rewrote this feature from scratch with a completely different internal structure but the same observable behaviour, would this test still pass? If not, you're testing the wrong thing.

Slow Tests Kill Discipline

A test suite that takes forty minutes to run is not a safety net — it's a ritual. Engineers stop running it locally. They push and hope. They batch their changes to avoid waiting. The feedback loop that testing is supposed to provide disappears entirely. Speed is not a nice-to-have for test suites. It directly determines whether the suite gets used. If your tests are slow, fixing that is more valuable than writing more tests. A fast suite of one hundred tests that runs in two minutes will catch more bugs in practice than a comprehensive suite of a thousand tests that nobody runs until CI.