NewsCrawler.org

The Kluever Maneuver: fixing bugs in two commits

The "Kluever Maneuver" is a habit of fixing bugs in two commits: the first commit adds a test that records the buggy behaviour and therefore passes, and the second commit fixes the bug and changes the test to the correct expectation. The simplest example is an add(a, b) function that returns a + b + 1: the first commit adds a test asserting add(2, 2) == 5, and the second changes the implementation to return a + b and the assertion to == 4. The two stated reasons are demonstrable proof that the bugfix actually fixed the bug, and CI[1] still passing at every commit.

The traditional way is to fix the bug and add a test in the same commit, but if that test already passed before the fix and the fix did not actually work, it gives false confidence. A subtler example is an age check built on today.year - birthday.year: it considers only years, so someone born in December 2008 is not yet 18 in October 2026 but still passes an 18 <= check. A plausible-looking fix treats the problem as an off-by-one error[2] and changes the comparison in can_access from 18 <= to 18 <, but the real defect is in the age calculation, and the test proposed with it passes both before and after that change. The Kluever Maneuver would have prevented this: the first commit wrongly asserts that someone born on New Year's Eve 2008 is eighteen on New Year's Day 2026, and the assertion passes on the buggy code. The second commit then fixes the age calculation with relativedelta(today, birthday).years from dateutil and flips the assertion to assert not.

The author also recommends logging the bug in a bug tracker[3]: the first commit references the ticket (FIXME: bug #1729) and the second records the fix (fixes: #1729). The name comes from Kurt Kluever, the author's colleague who taught him the technique; he calls such tests "terrible tests" — tests asserting something you know to be wrong.

Read the original jordi.inversethought.comDiscussion

Sources

Published: First seen: Читать по-русски

Open as a page