I have lost count of how many times I stared at a piece of code for an hour, convinced it was broken, only to discover the code was fine and the test was lying to me.

The test asserted the wrong thing. It set up the wrong data. It depended on the order other tests ran in. It passed for months by accident and failed the day it mattered. And every minute I spent doubting the code was a minute the real problem — a test I trusted too much — went unexamined.

Tests are code. They have bugs like any code. The difference is that when application code has a bug, something visibly breaks. When a test has a bug, it breaks quietly, by telling you the wrong thing with total confidence.

The three ways tests lie

The first lie is the test that passes for the wrong reason. It calls the function, something throws, the assertion never runs, and the framework reports a pass because no assertion failed. Or it checks a value that is always true no matter what the code does — asserting that a list is defined when it should assert what is in the list. These tests are worse than no test at all, because they give you confidence you have not earned.

The second lie is the test that depends on hidden state. It passes when the whole suite runs in order and fails when you run it alone, or the other way around. A database row left over from a previous test, a mocked clock that was never restored, a global variable one test sets and another reads. I once spent a whole evening on a test that only failed on Mondays — it was comparing dates, and the fixture data was written on a Friday.

The third lie is the test that tests the implementation instead of the behavior. It asserts that a function calls another function three times, or that an internal cache has a certain size. Then someone refactors the internals — the behavior stays exactly the same — and the test fails. Now the team has a choice: fix the code that is not broken, or rewrite the test. Most teams quietly weaken the test, and the suite gets a little less honest each time.

How I write tests I can trust

The rule that fixed most of this for me is simple: a test should fail if and only if the behavior it describes is broken. Before I trust a new test, I break the code on purpose and watch the test fail. If it stays green while the behavior is wrong, the test is decoration, and I rewrite it until it catches the break.

I also write the assertion first, in plain language, before I write the setup: "submitting twice creates one record." Then the test is a check of an outcome, not a transcript of whatever the code happens to do. And I run new tests alone, not just in the suite, because a test that only passes in the company of other tests is telling you about the suite, not about your code.

Finally, I delete tests that no longer describe anything real. A test suite is a living document, and a stale test is a liar with seniority. Nobody wants to delete a test, because it feels like deleting safety. But a test that asserts outdated behavior is not safety — it is a tripwire on a path you no longer walk.

Trust your tests, but verify them the same way you verify anything else in the codebase: by watching them fail when they should. A test that has never failed is a test you have never actually tested.