自动修复 Bug:从错误信息到补丁的程序修复

FreeGuideOnline 最新 2026-06-25

python def divide(a, b): return a / b ```

  • 一个会触发错误的测试文件 test_calculator.py
    import calculator
    
    def test_divide_by_zero():
        assert calculator.divide(10, 0) == "Division by zero error"
    
  • 安装自动修复工具(假设已安装 auto-fix 命令行)。

步骤 1:运行测试并获取错误信息

pytest test_calculator.py

输出显示 ZeroDivisionError,测试失败。

步骤 2:启动自动修复流程

auto-fix --source calculator.py --test test_calculator.py

工具会:

  • 根据错误栈定位到 calculator.pydivide 函数。
  • 结合测试中“期望返回字符串”的语义,尝试生成补丁候选。

步骤 3:查看生成的补丁

以 diff 格式呈现:

@@ -1,3 +1,5 @@
 def divide(a, b):
-    return a / b
+    if b == 0:
+        return "Division by zero error"
+    return a / b