返回资讯中心
外部精选
软件工程
#开发者实时流

Kluever 技巧:修 bug 应该拆成两次提交

来源标题:The Kluever Maneuver

开发者 Kluever 提出一种修复 bug 的提交方式:先提交一个能复现错误的测试,再提交修复代码。这样可避免测试在修复前就已通过、从而误以为修复有效。该方法被称为 Kluever Maneuver。

Hacker News 最新birdculture2026/11/1 · 16 天前14 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

简而言之:每当你做 bugfix 时,把它拆成两个 commit 来完成。

def add(a, b):
 return a + b + 1

凭借你敏锐的双眼,你发现这个加法函数有 bug。于是你写一个测试,把这种错误行为记录下来,

Commit 1

diff --git a/test_arith.py b/test_arith.py
--- a/test_arith.py
+++ b/test_arith.py
+def test_add(a, b):
+ assert add(2, 2) == 5

然后是 bugfix,

Commit 2

diff --git a/arith.py b/arith.py
--- a/arith.py
+++ b/arith.py
 def add(a, b):
- return a + b + 1
+ return a + b
diff --git a/test_arith.py b/test_arith.py
--- a/test_arith.py
+++ b/test_arith.py
 def test_add(a, b):
- assert add(2, 2) == 5
+ assert add(2, 2) == 4

但为什么要这样?

三个理由:

传统做法是把 bugfix 和测试放在同一个 commit 里。但如果这个测试碰巧在所谓的 bugfix 之前就已经通过了,而所谓的 bugfix 实际上根本没起作用呢?在这种情况下,新增的测试会给人一种虚假的信心,让人以为 bugfix 生效了。

假设我们要确保用户没有未成年,

def age_in_years(birthday, today):
 return today.year - birthday.year

def can_access(birthday, today):
 return 18 <= age_in_years(
 birthday,
 today
 )

你注意到这个年龄检查有 bug。它只考虑了年份。如果现在是 2026 年 10 月,但你出生于 2008 年 12 月,那你还没满 18 岁。

要修复它,问题只是一个 off-by-one。

diff --git a/dates.py b/dates.py
--- a/dates.py
+++ b/dates.py
 return today.year - birthday.year
 
 def can_access(birthday, today):
- return 18 <= age_in_years(
+ return 18 < age_in_years(
 birthday,
 today
 )
diff --git a/test_date.py b/test_date.py
--- a/test_date.py
+++ b/test_date.py
+def test_can_access():
+ assert can_access(
+ date(2007, 1, 1),
+ date(2026, 1, 1)
+ )

敏锐的读者会发现,问题不在 can_access 函数里,而在年龄计算里。这个 bugfix 并不是真正的 bugfix,而提出的测试在所谓修复之前和之后实际上都能通过。

Kluever Maneuver 本可以避免这种情况。

我们错误地检查了一个 2008 年除夕出生的人在 2026 年元旦就已经十八岁了。

commit 1

diff --git a/test_date.py b/test_date.py
--- a/test_date.py
+++ b/test_date.py
+def test_can_access():
+ assert can_access(
+ date(2008, 12, 31),
+ date(2026, 1, 1)
+ )

然后我们修复年龄计算,并把测试的断言翻转过来。

commit 2

diff --git a/dates.py b/dates.py
--- a/dates.py
+++ b/dates.py
+from dateutil.relativedelta import relativedelta
 def age_in_years(birthday, today):
- return today.year - birthday.year
+ return relativedelta(
+ today,
+ birthday
+ ).years
 
 def can_access(birthday, today):
 return 18 <= age_in_years(
diff --git a/test_date.py b/test_date.py
--- a/test_date.py
+++ b/test_date.py
 def test_can_access():
- assert test_can_access(
+ assert not test_can_access(
 date(2008, 12, 31),
 date(2026, 1, 1)
 )

我们不应该改用 XFAIL 吗?

好吧,首先,不是每个测试框架都有 XFAIL 的等价物(唉,Java……)。

撇开这一点不谈,一个正向通过的测试在逻辑上并不完全等同于一个正向失败的测试。回到我们的加法例子,如果我们加上这个测试,

Commit 1

diff --git a/test_arith.py b/test_arith.py
--- a/test_arith.py
+++ b/test_arith.py
+@pytest.mark.xfail
+def test_add(a, b):
+ assert add(2, 2) == 4

这个测试只告诉我们当前的加法结果不是四,但并没有告诉我们加法结果到底是什么。根据 bug 的性质,一个正向失败的测试可能无法那么清晰地说明 bug 到底是什么。知道二加二等于五,也许能让人更容易理解该如何修复这个 bug。

话虽如此,使用 XFAIL 大体上是差不多的。如果由于某种原因我们现在无法立刻修复这个 bug,只能把它记录下来,那么 XFAIL 更好,因为我们预期这个问题会伴随我们更长时间。

啊,对了,在你的 bug tracker 里记录一个 bug 也是个好主意。

add a test showing the bug
diff --git a/test_arith.py b/test_arith.py
--- a/test_arith.py
+++ b/test_arith.py
+FIXME: bug #1729
+def test_add(a, b):
+ assert add(2, 2) == 5

在第二个 commit 里记录修复:

remove off-by-one error
fixes: #1729
diff --git a/arith.py b/arith.py
--- a/arith.py
+++ b/arith.py
 def add(a, b):
- return a + b + 1
+ return a + b
diff --git a/test_arith.py b/test_arith.py
--- a/test_arith.py
+++ b/test_arith.py
-FIXME: bug #1729
 def test_add(a, b):
- assert add(2, 2) == 5
+ assert add(2, 2) == 4

好吧,但为什么叫「Kluever Maneuver」?

哦。因为我的同事 Kurt Kluever 教会了我这个方法,我一想到这个技巧就立刻想起他。他通常称之为「糟糕的测试」,比如「Jordi,你能为这个写个糟糕的测试吗?」然后我就去写一个断言某个我明知是错误的东西的测试。

墨西哥自由软件爱好者和数学家。

你的电子邮件地址不会被公开。必填项已用 * 标注

当前你的 JavaScript 已被禁用。要发表评论,请确保已启用 JavaScript 和 Cookie,然后重新加载页面。点击此处了解如何在浏览器中启用 JavaScript。

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →