I wrote a test rule that compares a constant with itself, and put it in everyone's instructions
I recently audited our test suite. Some tests were slow because they waited on real time: real grace periods, real poll intervals, real retry delays. The fix for that is well known, and I wrote it into the shared coding guidelines that every contributor reads before writing a test. At the end of that sentence I added a clause that nobody had asked for:
…a duration the code waits is a parameter the test sets short, or a clock the test controls, with one assertion pinning the production value.
That clause was a mistake. I also added it to the guidelines without the owner’s approval, inside a commit that was mostly about filing issues from the audit. The owner only found out because a later report quoted it. This post covers both problems.
What the clause asks for
In code, “pinning the production value” can only mean something like this:
assert_eq!(Config::default().timeout, Duration::from_secs(5));
The 5 on the right is the same literal the Default implementation already holds. Tests of this shape were already in the codebase. My rule didn’t invent the pattern. It made it official and told everyone to write more of it.
Why it’s wrong
- It checks nothing. Both sides are the same constant, typed twice. It fails only when someone edits one copy and not the other, and the only “fix” is to retype the other copy. If a test can only be satisfied by retyping the value it tests, it isn’t testing anything.
- It copies an implementation detail. If the design doesn’t state the value, the value can change freely. A second copy in a test turns a one-line change into a two-file change and takes away freedom the design allows.
- It’s why small retunes spread across many files. This has happened many times: someone tunes a single default, and every test that pinned the old number breaks. None of those failures ever found a bug. The more faithfully my “one assertion per duration” rule was followed, the bigger the damage from every retune.
- It gives false confidence. I justified it as “the real default is still checked.” It isn’t. The assertion shows the number was typed the same way twice. It doesn’t show the number is right, that it’s used, or that it has any effect. The behaviour that matters is that the code waits the configured time and then acts. The short-parameter or controlled-clock test already covers that, whatever the value is.
The part that worried me most
Later, someone rewrote the audit report for a wider audience. They worked outside the codebase and had never seen my rule. In the section on what should have been written, they prescribed the same thing in their own words: “with one assertion pinning the production value.”
So this isn’t one bad rule that got copied. It’s what we tend to produce by default whenever someone asks how a duration should be tested. Deleting my clause stops encouraging the habit, but the habit stays. The next review, remedy or policy draft will add “and one test pins the real value” unless something explicitly says not to.
Two people reaching the same practice independently doesn’t prove the practice is right. Here it only showed that we share the same bad default.
What should have been written
The first half of the rule stays: a duration the code waits on is a parameter the test sets short, or a clock the test controls. The pinning clause goes, and this rule replaces it, written as a prohibition on purpose:
A production constant is written once. A test does not assert that a default equals its own literal. If a constant’s value matters to users or to another component, the design states it, and the test checks the behaviour the design describes, not the number. If the design doesn’t state it, it’s an implementation detail and falls under DRY: no second copy, in a test or anywhere else.
In practice:
- For a default timeout: no assertion on the number. The real test sets the timeout to a few milliseconds and checks that the action happens after it elapses and not before. The production number never appears.
- For existing literal-equals-default lines: delete them. The same applies to tests that repeat a settings table’s keys and defaults. If they’re a contract that another part of the system reads, test them through that part. Otherwise drop them.
- For a value users depend on, like a documented client timeout: the design states the number, and a conformance test cites that requirement. Changing the number is then a design change and gets reviewed as one. That’s the only case where a test should hold the literal.
The rule has to be stated as a “don’t”. If the guidelines say nothing, people fall back on the default, and the default is the anti-pattern.
On changing the guidelines without asking
The shared guidelines are the owner’s instructions to everyone who writes code here. A sentence added there doesn’t produce one bad test. It produces every bad test from then on. I treated a rule I came up with during an audit as if it were policy. It was a proposal. From now on, if I think a fix needs a new rule, I’ll describe the rule to the owner and let them decide. Any change to the guidelines goes in its own commit with the owner’s explicit approval, not inside a commit about something else.
What I’ll look for in review
- Any assertion whose two sides are the same constant written twice, like
assert_eq!(T::default().x, <literal>)orexpect(DEFAULT_X).toBe(<literal>). - Wording like “so the default is still checked” or “pin the production value”. I’ll ask what bug the pin would catch. If the only answer is “the value changing”, the pin is the bug.
- A numeric literal in a test that matches a production constant when no design requirement states that number.
- A change to one constant whose diff touches more than the file that defines it. Each extra file is a duplicate to remove, not to update.
- Remedies and “should have been written” sections that introduce a new practice. They get held to the same standard as code: any test they prescribe has to name the failure it detects.
- Changes to shared guidelines that the owner didn’t ask for, or that arrive inside unrelated commits.
- Reports that cite a guideline as their authority. I’ll check who added it and when. A rule written by the same audit isn’t independent support for that audit’s conclusions.
The broader lesson: when a fix sounds like common sense (“still check the real default”), that’s when I most need to ask what it actually detects.