A widely held belief says that the more a test checks, the better it is. One test that logs in, creates an order, edits it, pays, downloads the invoice and verifies twenty fields looks efficient. Look how much it covers! One test, one report line, one setup.
Then it fails, and you spend an hour finding which of the twenty-odd things went wrong. After that it fails again for a reason unrelated to the invoice, and a real invoice bug stays hidden behind the earlier failure. The belief is backwards. A good test is small, and it checks one idea so clearly that a failure explains itself.
Three parts, in order
The shape most teams settle on is Arrange, Act, Assert (some say Given, When, Then; the idea is the same). Arrange: put the system in the starting state. Act: do the one thing under test. Assert: check the outcome.
Take a coupon rule: a coupon worth ₹200 applies only if the cart is at least ₹1,500. Arrange a cart of ₹1,499 and the coupon. Act by applying it. Assert that it is rejected with a message mentioning the minimum, and that the cart total is unchanged. Then a sibling test at ₹1,500 which is accepted, with a total of ₹1,300.
Name the test after the behaviour, not the method: “coupon is rejected when cart is below minimum” tells a stranger what broke before they open any code. A name like “test_coupon_3” tells them nothing.
The structure also exposes problems. If Act has three steps, you are testing a journey, not a behaviour. If the Assert section checks six unrelated things, the test has several reasons to fail. And if Arrange is forty lines long, the code under test needs too much context, which is a hint about the design, not a reason to copy the setup around.
Smells worth recognising
The term “test smell” comes from the refactoring community: a surface sign that something deeper is off. A few that appear in nearly every codebase.
The giant test, described above. Fix by splitting along behaviours.
Tests that depend on each other. Test B only passes if Test A ran first and left a record behind. Run them in a different order, or in parallel, and half fail. Every test should build the state it needs and not rely on neighbours.
Logic inside the test. Loops and conditionals in a test mean the test itself can be wrong, and then who tests the test? If you need a calculation to produce the expected value, you may be copying the production formula, in which case the test agrees with the code even when both are wrong. Prefer a hand-worked expected value: ₹1,300, written as a plain number.
Assertion-free or weak assertions. A test that calls the code and checks only that nothing crashed will pass when the answer is wrong. Likewise “result is not null” when the point is what the result contains.
Sleeps and fixed waits. A pause of five seconds hoping a background job has finished slows the suite and still fails on a busy day. Wait on a condition instead, with a timeout.
Over-mocking. When a test replaces every collaborator with a mock and then verifies the mocks were called in a particular order, it describes how the code works, not what it achieves. Refactor the code without changing behaviour and the test breaks. Tests like this punish improvement.
Hidden magic values. The test creates “customer 7” and expects a discount, and nobody knows why 7 qualifies. Name the data after its meaning: a customer with more than ten orders.
Duplication: the one place to relax
Developers are taught to avoid repetition, and carry that into test code, extracting shared helpers and base classes until a test’s meaning is spread over five files. Here I would loosen the rule. In tests, readability beats cleverness. A little repeated setup that keeps the test understandable in one screen is usually better than a clever shared fixture you must trace to understand. Extract when the repetition hides nothing, such as a builder that creates a default valid customer so each test mentions only the field it cares about.
A cheap review habit
When reviewing a colleague’s test, ask three things. Could I explain what this test proves in one sentence? If I broke the feature in an obvious way, would this test fail, and fail with a clear message? If it fails in six months and the author has left, could someone work out why? You can even try the middle one literally: deliberately break the production code and see whether the test notices. That is the idea mutation testing automates.
Tests get read far more often than they get written, mostly at the worst moment, when a build is red and someone is waiting. Write for that reader.
Figures and names in the examples are illustrative.
Leave a Reply