Engineering Practice
Add a Regression Test That Fails for the Original Bug
Prove that a regression test rejects the original defect before accepting a repair. An actual local Python example preserves an unchanged expectation, records the relevant failure and checks adjacent boundaries without claiming full application coverage.
Add a regression test by stating the intended behavior, reproducing the original defect and running the test against the broken implementation. Inspect the failure to confirm it reaches the intended assertion, then apply the fix and rerun the unchanged expectation. Finally, check neighboring boundaries and existing error behavior. A test that passes only on the current code has not yet shown that it would catch the bug returning.
This LandOffer.ai guide uses an original elapsed-hours function and actual local Python tests. The synthetic example is not a defect found in a real product or code suggested by an AI tool. No application, database, browser or production system was exercised. Primary guidance was checked October 27, 2026; the saved runtime records Python 3.12.14.

Define the failure in terms of behavior
State the expected result before choosing the repair. Include the input conditions, expected result and actual result. If the requirement itself is uncertain, resolve that question with the appropriate owner; encoding today's output as the expected value would preserve the defect rather than protect the intended behavior.
The fictional behavior here is deliberately narrow: given two UTC-aware timestamps in chronological order, report completed elapsed hours, including whole days. A partial hour is rounded down. A reversed interval keeps an existing ValueError. This is not a billing, business-hours, payroll or service-level calculation, and it does not count local calendar days.
Django's version 5.2 testing tutorial demonstrates exposing a future-publication-date bug with a failing test before changing the implementation. Its example is different from ours. The relevant method is to establish the defect and expected behavior, then preserve that expectation through the fix rather than treating a green result as enough by itself.
Choose the smallest useful reproduction. A complex fixture with unrelated network calls or database setup can fail before it reaches the behavior you mean to protect. Minimize those distractions while retaining the condition that triggers the bug. For this example, crossing a twenty-four-hour boundary matters; a same-day interval alone would not expose the problem.
Worked asset: show the test failing on the original function
The original synthetic implementation reads:
def whole_elapsed_hours(start, end):
if end < start:
raise ValueError('end precedes start')
return (end - start).seconds // 3600
It can appear correct for short durations. Python's 3.12 datetime documentation explains that a timedelta stores normalized days, seconds and microseconds, with the seconds component below one day. It explicitly cautions against confusing that component with the whole duration. The underlying library is behaving as documented; the bug is our use of the component for the specified result.
The regression input starts November 3, 2026 at 09:00 UTC and ends November 4 at 10:00 UTC. That is twenty-five elapsed hours. The expected value 25 follows from the stated behavior and these dates; it is not computed by repeating the function's internal expression in the assertion.
The relevant test method in the saved standard-library suite is:
class ElapsedHoursTests(unittest.TestCase):
def test_keeps_hours_beyond_one_day(self):
end = datetime(2026, 11, 4, 10, 0, tzinfo=timezone.utc)
self.assertEqual(whole_elapsed_hours(START, end), 25)
The actual file imports unittest, datetime, timedelta, timezone and the function. Its fixed START is datetime(2026, 11, 3, 9, 0, tzinfo=timezone.utc). It does not read the current clock. The suite also contains the adjacent-case and reversed-interval tests described below; the displayed excerpt isolates the regression method rather than pretending to be a standalone test file.
The local driver selects the original module and runs that single named test. It returns exit code 1, with the relevant failure:
AssertionError: 1 != 25
This is an actual assertion failure saved from the run, not an invented terminal example. The test found and called the intended function; it did not merely fail because an import was missing. That distinction matters. A syntax error or broken fixture also makes a command red, but would not establish that the test detects this elapsed-hours defect.
Keep the reproduction and its expected value stable while evaluating the fix. If the failing result makes you change the assertion from 25 to 1 without revisiting the requirement, you have changed the contract to match the bug. That disagreement is the point of the test: it must be able to reject code that returns the wrong completed-hours value, even when the code runs without an exception.
Apply the fix without rewriting the expected behavior
The revised function retains the existing guard and changes the duration calculation:
def whole_elapsed_hours(start, end):
if end < start:
raise ValueError('end precedes start')
return int((end - start).total_seconds() // 3600)
The same Python documentation describes total_seconds() as the duration's total seconds. The floor division implements this example's completed-hours rule. This does not establish a general date/time utility for arbitrary time zones, extremely long intervals, alternate rounding policies or malformed inputs; those contracts need their own choices and tests.
The driver reruns the exact same named test against the fixed module. It returns exit code 0. The driver hashes the same test file before the comparison and after each selected subprocess run, and asserts that every hash matches. The original and fixed modules are kept in separate local folders so the comparison remains inspectable; selecting the target module changes the implementation under test, not the expected answer.
Google's Test Behavior, Not Implementation article, by Andrew Trenk on August 5, 2013, recommends emphasizing observable behavior rather than exposing implementation details unnecessarily. Here the test calls the function and checks the result. It does not assert that the fix must invoke total_seconds, so a different correct implementation could satisfy the same behavior.
A passing targeted test establishes that this example now returns the expected result. It does not establish that every caller passes UTC-aware timestamps or that a user interface displays the value correctly. Keep the unit result at its actual level and identify any integration evidence the real change would require.

Worked asset: check the neighboring boundaries
The second completed asset is the actual local boundary matrix. All intervals start at the same fixed UTC timestamp. Expected values are literals chosen from the contract. The original and fixed columns show direct calls to the respective synthetic functions, while the fixed suite additionally asserts these expected values using unittest subtests.
| Interval supplied | Expected whole hours | Original observed | Fixed observed | Reason to keep the case |
|---|---|---|---|---|
| Zero minutes | 0 | 0 | 0 | Empty duration remains zero |
| 59 minutes | 0 | 0 | 0 | A partial first hour does not round up |
| 150 minutes | 2 | 2 | 2 | Ordinary same-day floor behavior remains intact |
| 1,439 minutes: 23 hours 59 minutes | 23 | 23 | 23 | Immediately below the day boundary still works |
| 1,440 minutes: exactly 24 hours | 24 | 0 | 24 | The exact boundary catches the discarded day |
| 1,500 minutes: 25 hours | 25 | 1 | 25 | The original regression case remains covered |
| 2,940 minutes: 49 hours | 49 | 1 | 49 | More than one day is retained, not just a one-day special case |
The reversed-interval test supplies November 3 at 08:59 UTC against the 09:00 UTC start and asserts ValueError with the existing message. It passes on the fixed function. That behavior is intentionally retained; the fix does not quietly reinterpret a negative interval or return a positive duration.
The full fixed run executes three test methods: the named regression, one method containing seven boundary subtests and the reversed-interval method. All pass. That is three test methods, not a claim that every boundary is a separate discovered test or that the suite covers all possible timestamps. The separate direct-call matrix records the original implementation's neighboring outputs as well.
The matrix explains the scope better than a bare “tests pass.” It shows why short same-day tests could miss the defect, why exactly twenty-four hours is important and why adding one special day would not cover forty-nine hours. It also preserves the floor behavior that the original already handled for ordinary small durations.
Keep the test independent enough to challenge the repair
A weak expected value can mirror the same mistake: calculating the expected answer with the duration's .seconds field would make both sides agree for the wrong reason. The literal 25 and the fixed input pair keep this test's oracle separate from the internal calculation. Review the expected behavior as carefully as the patch.
Avoid asserting incidental details that the requirement does not depend on. An internal helper call, temporary variable name or exact arrangement of arithmetic can change while the public result remains correct. Conversely, if a real requirement concerns an interaction such as avoiding an external write, test that relevant interaction deliberately rather than assuming every implementation detail is irrelevant.
The Python 3.12 unittest documentation describes assertions for expected values and exceptions, plus targeted test selection. Those features support this small before/after comparison. They do not determine the right contract, the correct fixture or whether a failure came from the intended path; the author still has to inspect the result.
Check which implementation actually loaded. This driver launches a fresh Python subprocess for each selected run, points its module search path at the intended original or fixed folder and preserves the code hashes. That avoids claiming a before/after comparison while accidentally reusing a previously imported fixed module. The saved failure also contains the named regression method, so a reviewer can connect the result to the intended assertion rather than an unrelated discovered test.
If the original revision is unavailable, say that the historical failure has not been demonstrated. You can still write a useful expectation and test the current fix, but the evidence claim is narrower. Retrieve an appropriate authorized revision or reconstruct a clearly labeled minimal defect when that helps review; do not describe a reconstruction as the actual historical production code. In this article, both implementations were created specifically for the teaching example.
A current-clock dependency can introduce another misleading red result. The fixed UTC pair makes the twenty-five-hour expectation stable across runs; no sleep, live clock or timing threshold is involved. That determinism supports this arithmetic check. A real caller could still parse or convert timestamps incorrectly. If that path appears in the original report, it needs its own reproduction rather than inheriting confidence from this arithmetic test.
Preserve enough information for another reviewer to reproduce the comparison: original and fixed code, unchanged test, runtime, command selection, exit statuses and relevant output. Keep the original failure beside the green result. Without it, a later reviewer sees that the repair passes, but loses the evidence that the test could reject the defect. In this article's evidence, both are saved together with hashes and the boundary matrix.
Decide what remains outside the regression claim
The local runs did not test daylight-saving transitions, mixed time zones, timestamp parsing, clock acquisition, persistence, UI formatting or extreme durations. They use small, explicit UTC-aware values. The example also does not show this test running in a product's continuous-integration system, or establish that every caller obeys its input contract.
For a real fix, add relevant integration or contract checks where the actual risk lives. If the reported bug comes from parsing timestamps incorrectly, testing only this arithmetic helper would miss that path. If callers need a different rounding policy, the function and expected values need an explicit product decision rather than a convenient test adjustment.
Before accepting a regression test, read the red result and explain why it corresponds to the original bug. Then show that the unchanged expectation passes on the repair and that the neighboring behaviors you intended to preserve still hold. LandOffer.ai publishes related engineering guidance; the useful result is a bounded, reproducible test claim that can challenge the defect returning.
Sources and Further Reading
- Django 5.2: Testing Tutorial — Expose a bug, inspect the failure, fix it and add neighboring cases; checked October 27, 2026.
- Python 3.12: datetime — Normalized duration components and total seconds; checked October 27.
- Python 3.12: unittest — Value/exception assertions, subtests and targeted selection; local runtime separately records Python 3.12.14.
- Andrew Trenk: Test Behavior, Not Implementation — Google Testing Blog, August 5, 2013.


