Software Engineering
Tests that pass without protecting anything: watch the test fail before you trust it
A green CI that ran no tests, a spec that never saw the template, and another that breaks between midnight and 1 a.m. I only trust a test after seeing it fail for the right reason.
Rodrigo Lucas · · 10 min read
In a Rails project, the CI test job was called test and was green on every pull request, except it wasn't running a single test. The command called Minitest, which looked in an empty test/ folder, while the real tests were RSpec and lived in spec/. When I changed the command, 111 examples ran in CI for the first time and 4 of them failed. Those 4 were already broken, and nothing said so.
That was the most glaring case, but I found three more tests, in another project, that passed without protecting what they claimed to protect. None of them was a bad test in the obvious sense. They had good names, ran fast and stayed green, and in all of them the problem was in what they couldn't see.
Putting the four together, the rule I kept is short. Green only means nothing failed, and zero tests executed is also zero failures. That's why I only trust a test after seeing it turn red for the right reason, by breaking on purpose the thing it claims to protect. The examples use RSpec, but the mechanism applies to any stack.
Zero tests is also zero failures#
A CI job turns green when its command ends with exit code 0, and it doesn't know how many tests should exist or whether the folder the runner read had anything in it. In the Rails project, at some point the tests moved to RSpec and the workflow didn't follow, so the job kept calling Rails' default runner. Minitest looked for tests, found none, reported zero runs and zero failures, and exited with 0. It honestly answered the question "did any test fail?", but the question that mattered was a different one, "did the tests someone wrote run and pass?", and no CI step was asking it.
RSpec has a setting for the extreme case, fail_if_no_examples, which makes the process exit with a failure when no example runs. The documentation recommends loading spec_helper through .rspec (with --require spec_helper) so the option applies even when no spec file is loaded. pytest already does this by default (it exits with code 5), and in Jest the danger is the opposite, turning on --passWithNoTests without knowing what you're accepting.
# spec/spec_helper.rb
RSpec.configure do |config|
config.fail_if_no_examples = true
end
There's a trade-off. Running a single file with a filter, or splitting the suite into shards, can produce a legitimately empty run, so sometimes the option only makes sense in the full job. And it doesn't catch the middle case, because a job that runs 3 examples out of 111 stays green. For that case what helped me was a habit. When I start working on a project, I open the test job log and look for the line with the count, which says which framework ran and how many examples it found. If the number doesn't match the size of the suite, the green tells you nothing.
What showed up when the suite ran#
Changing the command to bundle exec rspec turned the CI red, and that was the best news of the day. The 4 specs that were already failing had fallen behind the way every test that never runs does, describing a system the code had already stopped being.
The surprise came from somewhere else, with 5 specs for admin screens that passed on my machine and failed only in CI. The cause was the Tailwind CSS, which is compiled from the code and isn't in git. On my machine it existed, built in some earlier session, but the CI runner starts from a clean checkout and the screens that depended on the file broke when rendering. To reproduce it, I deleted the generated file, ran again, and the same 5 failed. The fix was a workflow step that compiles the CSS before the suite.
A test can be green on my machine because of state only my machine has, and the clean CI is the environment that exposes it, as long as it runs the tests. I also kept the rule that if the test framework changes, the workflow changes in the same pull request, because the half-done switch is what produced the fake green.
A spec that stopped before the break#
The other three cases came from a Rails monolith in production. I was fixing a search that returned 404 under certain conditions, and the first commit of the fix swapped the 404 for a 500. The new spec I wrote for the fix passed, and the bug was only caught later, outside the test.
The spec was a controller spec and checked the response status. In rspec-rails, a controller spec replaces the views with a template that renders an empty string, which is the default and makes sense for isolating the controller. My fix made the action return early and render a whole template without a variable it expected. With the view stubbed, the template was never evaluated, the break never happened, and the spec saw only the status.
What bothered me most afterwards was realizing that the "run it without the fix" ritual wouldn't have saved me. If I had undone the commit, the 404 would come back and the spec would turn red, but for the wrong reason. I would have seen the red, then the green, and walked away confident. The red I needed to see was the one from the break I feared, the template without the variable, and the spec couldn't produce that one.
The render_views declaration turns off the stub for the example group.
RSpec.describe SearchController, type: :controller do
render_views
it "renders the form when the search finds nothing" do
get :index, params: { term: "nonexistent" }
expect(response).to have_http_status(:ok)
expect(response.body).to include("No results")
end
end
With it the template runs and the error shows up as a 500 in the spec itself. The second expect line also counts, because checking only the status protects little when the change touches what the page shows. I don't turn render_views on everywhere, because a controller spec that renders the view gets slower and breaks when someone changes a piece of text on the page. I turn it on when the change affects what the action renders. Every kind of test swaps part of the system for a double to stay fast, and before trusting a test for a fix I ask whether the step I changed is inside or outside what it executes.
Two examples that read a variable#
In the same monolith, two examples in a controller spec proved that a flag hid certain items from the screen. To do that they read an instance variable of the controller with instance_variable_get and checked its value.
It looks reasonable until you try to break it. If the view starts reading another variable, or ignores the existing one, the two examples stay green and the screen shows exactly what the flag should hide. The test looked at the middle of the road and left out what the user sees. Rails, by the way, had already been through this discussion. The assigns and assert_template helpers, which read the controller's variables and the chosen template, left the framework in Rails 5 and moved to a separate gem, and instance_variable_get is the same coupling under another name.
The way out was to delete the two examples, because the flag's rule was already covered in the model test and what appears on screen already had a view spec. Deleting a test looks like a regression, and I only did it because there was coverage elsewhere. Without it, the right move would be to rewrite the test to look at the screen.
Since then I ask two questions before trusting a test. If I change the implementation without changing the behavior, does it break? If it does, it's too coupled. If I break the behavior, does it flag it? If it doesn't, it protects nothing, and the flag examples failed both.
A red that depended on the time of day#
The last case is the reverse of the others, a false red. A CI shard turned red twice in a row on a pull request of mine, even after re-running only the failed jobs, and my diff didn't touch that area. It was around 00:18 when I ran the same spec on the main branch, on my machine, and it failed there too.
The code under test was a job that looked for records created between midnight today and five minutes ago. The spec created records from 30 and 60 minutes ago and expected the job to find them, which works for 23 hours of the day. Between midnight and 1 a.m., "one hour ago" falls on the previous day, outside the window, and the spec breaks. After 1 a.m. it passes again. The failure is deterministic by the time of day, unlike network flakiness, and running it again three times in the same hour only confirms the red.
The root is the same as the CSS false green, a test that depends on something it doesn't control. There it was a generated file, here it's the clock, and in both the result says more about the environment than about the code. For a spec with relative time, the fix is to freeze the clock at a time far from the edge. Rails' travel_to replaces Time.now, Date.today, and similar with the instant you choose and undoes it at the end of the block.
around do |example|
travel_to(Time.zone.parse("2026-01-15 14:00")) { example.run }
end
Freezing at 2 p.m. protects the test, but it also hides the edge, and the job itself may have a real bug in the first hour of the day. That's why the edge deserves its own example, frozen at 00:20, stating what the expected behavior is. And when a spec with a date window fails with no relation to the diff, the first thing I compare is the time of the run with the window in the code, before blaming the pull request.
Red for the right reason#
What the cases have in common is that nobody had seen those tests fail for the reason that mattered. The RSpec suite had never run in CI. The search spec had never seen the template break, and the red it knew how to produce was the 404 one. The flag examples had never seen the wrong screen. Even the midnight spec had the same problem in reverse, because the red it showed came from the clock and not from the code.
Watching the test fail before the fix is the red, green cycle of writing the test first, and it also applies when the test comes after. But the search case taught me to be pickier about that red. Undoing the fix shows that the test notices the difference between before and after. To know whether it catches the break I fear, I cause that break (remove the variable from the template, make the view ignore the flag) and read the failure message. If it fails for another reason, or doesn't fail, it still doesn't protect what I wanted.
For the whole suite the check is cruder and done once per project. An expect(1).to eq(2) in any spec, in a throwaway commit, has to turn the CI red. If it doesn't, you find out in five minutes what nobody in that Rails project had seen.
Mutation testing (with tools like Stryker) automates the same idea, changing the code on purpose and expecting some test to fail, at the price of running the suite once per change. Coverage helps less, because it says which lines ran and not whether any assertion looked at their result, and the flag examples covered the whole controller without protecting the screen.
The part I still haven't solved is the middle case, a suite that runs only a fraction of its examples and stays green. None of the options above catches it on its own, and today I rely on reading the count in the log. If you can do only one thing today, make the throwaway commit with expect(1).to eq(2) in the project you work on and see what color the CI turns.
References#
- RSpec: Setting the fail_if_no_examples option
- rspec-rails: views are stubbed by default (controller specs)
- rspec-rails: render_views
- pytest: Exit codes
- Jest CLI: --passWithNoTests
- Rails API: ActiveSupport::Testing::TimeHelpers (travel_to)
- rails-controller-testing: assigns and assert_template removed in Rails 5
- Stryker Mutator: what mutation testing is
updated on
01 blog
related
software engineering
A perfect Lighthouse SEO score does not prove SEO, the HTML your site serves does
Lighthouse gave my site an SEO score of 100 while the canonical pointed to a domain that returned 404. What the audit checks, what it cannot check and a 40-line checker that catches the defect.
software engineering
Prompt caching in practice: the obvious cut that would have saved 4%
The goal was to cut 20% of an AI agent's cost with a prompt router. Measuring showed about 4%, because 63% of the input already came from the cache, billed at 10% of the price.
software engineering
Your agent eval moves more than your improvement
Two runs of the same eval, with nothing changed, gave 94.5% and 93%. How I measured an AI agent with checks written in code only, and where the number fooled me.
02 blog
Who writes
Contact
Let's swap ideas
Get the next articles
No spam, one email when a new article ships. Unsubscribe anytime.