Regression Test Still Passing After Reverting a Fix: Why?
In this article
If your regression test still passes after you’ve reverted the code fix it was meant to verify, the most likely reason is that the test never actually exercised the changed behavior in the first place. You need to trace the test’s data flow, assertions, and environment to find where it became disconnected from the code you reverted.
What a Regression Test Is Supposed to Do
A regression test is a safety net. Its job is to catch when a previously fixed bug reappears. When you revert a fix, you are intentionally re‑introducing the bug. If the test still passes, the net has a hole. The test is not checking what you think it is checking.
This situation is common in legacy codebases, especially when tests were written hastily or when the original bug was fixed without a test. It also happens when tests rely on mocks that bypass the real logic, or when the test’s assertions are too weak to detect the difference.
First Step: Verify the Revert Actually Removed the Fix
Before diving into the test, make sure your revert did what you intended. A common mistake is reverting the wrong commit, or reverting only part of a multi‑file change. Check the following:
- Run
git diffto confirm the fix’s code is gone. - Search for the function or variable that the fix introduced or modified. If it’s still referenced, you may have reverted only a portion.
- Rebuild the project cleanly. Some languages (like C++ or Java) cache compiled artifacts; a stale build can hide the revert.
- Run the application manually and reproduce the original bug scenario. If the bug does not reappear, the revert is incomplete.
If the bug is truly back, but the test still passes, move on to the test itself.
Trace the Test’s Execution Path
Now you need to follow exactly what the test does. Start by reading the test code line by line. Ask these questions:
- Does the test call the same function that was fixed? Sometimes a test calls a wrapper that no longer uses the fixed code.
- Does the test use a mock or stub for the class or module that contained the fix? If so, the mock bypasses the real implementation entirely.
- Does the test set up input data that avoids the buggy path? For example, if the bug only appears with negative numbers, but the test uses positive ones, it will pass both before and after.
- Does the test assert on the correct output? A test that checks for an error message when it should check for a return value can pass even when the logic is wrong.
Use a debugger or add temporary logging to see what values the test actually produces. Compare those values with what you’d expect if the bug were present.
Common Reasons a Test Passes After Reverting a Fix
Here are the most frequent culprits, in rough order of likelihood:
- The test doesn’t cover the fixed behavior. The fix addressed a specific edge case, but the test only checks the happy path. For example, a fix might handle a null input, but the test always passes a non‑null value.
- The test is too lenient. The assertion checks for a type or a range that is true in both the buggy and fixed versions. For instance, asserting that a function returns a string, when the bug caused it to return an empty string, would pass either way.
- The test is skipped or ignored. A test marked with
@Ignore(JUnit),@pytest.mark.skip, or similar will always pass. Check for skip annotations or conditional skips. - The test uses a mock that replaces the fixed code. If the test mocks the class or method that was changed, it’s not testing the real implementation. This is common in unit tests that isolate dependencies.
- The test runs against a different version of the code. A test might be pointing to a deployed artifact or a different branch. Verify that the test runner is using the code you reverted.
- The test has a bug itself. A typo in an assertion, a wrong variable, or a missing assertion can make a test pass vacuously. For example,
assertTrue(true)always passes.
How to Find the Disconnect
Once you suspect the test isn’t covering the fix, you can prove it with a simple experiment: temporarily break the code in a way that should fail the test. For example, if the fix was about handling a null input, add a line that throws an exception on null. If the test still passes, you’ve confirmed it’s not exercising that path.
Another technique is to use code coverage tools. Run the test with coverage enabled and look at which lines of the fixed code are actually executed. If the lines that were changed are not covered, the test is not testing the fix.
You can also write a new, minimal test that directly calls the fixed function with the exact input that triggered the original bug. Run it against the reverted code – it should fail. If it doesn’t, then the bug isn’t what you thought it was, or the revert didn’t reintroduce it.
Fixing the Test
Once you’ve identified why the test is ineffective, you have a few options:
- Update the test to include the edge case that the fix addressed. Add a specific input that would fail without the fix.
- Strengthen assertions to check for exact values or conditions that distinguish correct from buggy behavior.
- Remove unnecessary mocks so the test exercises the real code path. If mocking is required for isolation, at least mock at a lower level so the fixed logic is still executed.
- Delete the test if it’s redundant and replace it with a proper regression test. A test that never fails is worse than no test, because it gives false confidence.
After updating the test, run it against both the reverted code (it should fail) and the fixed code (it should pass). This confirms the test is now meaningful.
Frequently Asked Questions
Why does my test pass even though the bug is clearly back?
Most likely the test doesn’t exercise the buggy code path. It may be using a mock, testing a different function, or checking an assertion that is true in both cases. Use code coverage and manual reproduction to confirm.
How can I tell if a test is actually testing what I think it is?
Read the test carefully and trace the inputs and outputs. Then deliberately break the code in a way that should fail the test. If the test still passes, it’s not covering that behavior. Coverage tools also help.
What if the test was written by someone else and I don’t understand it?
Start by running it with a debugger and stepping through. Look at the test name and any comments. If it’s still unclear, write a new test that directly tests the fixed behavior, and consider replacing the old one.
Is it okay to just delete a regression test that doesn’t work?
Only if you replace it with a meaningful one. A test that always passes gives false confidence. Delete it and write a new test that actually fails when the bug is present.
Can a test pass because of caching or build issues?
Yes. If your build system doesn’t recompile changed files, the test may run against stale code. Do a clean build and ensure the test runner uses the correct artifacts.
What if the test is flaky and sometimes passes, sometimes fails?
Flakiness suggests a different problem, like race conditions or environment dependencies. But if it always passes after a revert, it’s probably not flaky – it’s just not testing what you think.