Ask ChatGPT for Python Tests That Catch the Original Bug
Passing tests tell you more when you first prove that those same tests reject the bug. This tutorial asks ChatGPT for standard-library Python tests around a fictional retry-delay function, preserves the original failing run, and challenges the suite with deliberate wrong implementations before accepting the repair.
Specify the public behavior
The function calculates a delay; it does not sleep or contact a service. Attempt numbers start at one. With default base 1 and cap 8, attempts one through five must return 1, 2, 4, 8, 8. The original defect doubles one step too early, returning 2 for the first attempt.
Write unittest tests importing retry_delay from retry.py.
Test observable outputs and exceptions, not source-code strings.
Attempts start at 1; default delays are 1, 2, 4, 8, 8.
Support positive finite custom base/cap values and cap below base.
Reject invalid attempts, booleans and invalid numeric parameters.
Require a very large attempt to return the cap promptly.
First run the tests against the unchanged broken implementation.Preserve the original retry.py. Save the returned suite as test_retry.py beside a disposable working copy. Read both before running them. Python’s unittest documentation explains assertions, exception checks and test discovery.
Make the original implementation fail
python3 -m unittest -v test_retryKeep the failing output. The first-attempt assertion must report the original off-by-one error. Checking only a large attempt already at the cap would miss it: both correct and incorrect implementations can return 8 there.
import unittest
from retry import retry_delay
class RetryContract(unittest.TestCase):
def test_default_sequence(self):
self.assertEqual(
[retry_delay(a) for a in range(1, 6)],
[1, 2, 4, 8, 8],
)
def test_invalid_first_attempt(self):
with self.assertRaises(ValueError):
retry_delay(0)This is an excerpt of the behavior being tested. The recorded suite also covers custom parameters, a cap below the base, very large attempts and invalid inputs. Preserve those tests rather than replacing the complete suite with this short illustration.
Run the same tests against the proposed repair
Save and inspect ChatGPT’s repaired function separately, then place a copy under the module name imported by the tests. Rerun the identical command. Do not change expected 1 to 2 to accommodate the broken first attempt. The requirements should decide whether the implementation passes.
With base 0.5 and cap 3, the demonstrated expected sequence is 0.5, 1, 2, 3, 3. If the base exceeds the cap, the first delay already equals the cap. Reject attempts such as zero, negative integers, fractions and booleans. Check positive finite base and cap values, including rejection of NaN and infinity. Both parameters still need validation even when one would make the return value look obvious.
Show what the suite rejects
Use separate disposable copies for deliberate mistakes. Try the original early-doubling bug, a version without the cap, a function that always returns a constant, and a version that accepts an invalid attempt. Run the unchanged suite against each. Every incorrect implementation should fail for the relevant behavior.
The recorded independent checker required the fixed implementation to pass and all four known wrong implementations to fail. It also checked that a very large attempt reaches the cap quickly. The reviewed fix stops doubling once capped instead of constructing a huge exponential value; there is no real-time waiting in these tests.
These examples establish the stated numerical contract, not a complete retry policy for live services. Real integrations also need rules for retryable errors, maximum attempts, jitter and cancellation. Keep the failure evidence and regression tests with your code. For another concrete invariant check, the SQLite integer-cents tutorial verifies exact totals rather than accepting a success message. Find more practical examples in Tutorials.
James Kennedy is a writer and product researcher at Drives Hero with a background in IT administration and consulting. He has hands-on experience with storage, networking, and system performance, and regularly improves and optimizes his home networking setup.

