test-fix

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test Fix

测试修复

Systematic approach to diagnosing and fixing failing tests.
针对失败测试的系统化诊断与修复方法。

Process

流程

Step 1: Identify Failing Tests

步骤1:定位失败的测试

bash
cargo test --all
cargo test test_name -- --exact --nocapture
bash
cargo test --all
cargo test test_name -- --exact --nocapture

Step 2: Reproduce Locally

步骤2:本地复现问题

bash
undefined
bash
undefined

With debug logging

启用调试日志

RUST_LOG=debug cargo test test_name
RUST_LOG=debug cargo test test_name

Force single-threaded for race conditions

强制单线程以排查竞态条件

cargo test test_name -- --test-threads=1
undefined
cargo test test_name -- --test-threads=1
undefined

Step 3: Diagnose Root Cause

步骤3:诊断根本原因

PatternSymptomFix
Async/Await"future cannot be sent"Add
.await
, use Arc<Mutex>
Database"connection refused"Check env vars, use test DB
RaceIntermittent assertion failureAdd Mutex, sequential execution
Type"expected X, found Y"Update signatures, add conversions
Lifetime"borrowed value"Clone data, adjust lifetimes
模式症状修复方案
Async/Await"future cannot be sent"添加
.await
,使用Arc<Mutex>
数据库"connection refused"检查环境变量,使用测试数据库
竞态条件间歇性断言失败添加Mutex,改为顺序执行
类型问题"expected X, found Y"更新签名,添加类型转换
生命周期问题"borrowed value"克隆数据,调整生命周期

Step 4: Verify Fix

步骤4:验证修复效果

bash
undefined
bash
undefined

Run multiple times

多次运行测试

for i in {1..10}; do cargo test test_name -- --exact || break; done
for i in {1..10}; do cargo test test_name -- --exact || break; done

Run full suite

运行完整测试套件

cargo test --all
undefined
cargo test --all
undefined

Step 5: Regression Prevention

步骤5:防止回归

Add test for the specific bug that was fixed.
为修复的特定Bug添加测试用例。

Debugging Checklist

调试检查清单

  • Run failing test in isolation
  • Check environment variables
  • Review recent changes
  • Check for missing
    .await
  • Verify database connections
  • Look for race conditions
  • Check type compatibility
  • 单独运行失败的测试
  • 检查环境变量
  • 查看最近的代码变更
  • 检查是否遗漏
    .await
  • 验证数据库连接
  • 排查竞态条件
  • 检查类型兼容性

Tools

工具

bash
undefined
bash
undefined

Debug logging

调试日志

RUST_LOG=debug cargo test
RUST_LOG=debug cargo test

Full backtrace

完整回溯信息

RUST_BACKTRACE=full cargo test
RUST_BACKTRACE=full cargo test

Specific module

特定模块日志

RUST_LOG=memory_core=debug cargo test
undefined
RUST_LOG=memory_core=debug cargo test
undefined

When to Skip vs Fix

何时跳过修复 vs 立即修复

Skip (temporarily):
  • External service down
  • Platform-specific issue
  • Known upstream bug
Fix immediately:
  • Logic error
  • Incorrect assertion
  • Missing error handling
暂时跳过
  • 外部服务宕机
  • 平台特定问题
  • 已知的上游Bug
立即修复
  • 逻辑错误
  • 断言不正确
  • 缺少错误处理