Loading...
Loading...
Compare original and translation side by side
Test Writing Progress:
- [ ] Step 1: Review project standards (check existing tests)
- [ ] Step 2: Understand behavior (what should it do? what can fail?)
- [ ] Step 3: Choose test type (Integration/E2E/Unit)
- [ ] Step 4: Identify dependencies (real vs mocked)
- [ ] Step 5: Write failing test first (TDD)
- [ ] Step 6: Implement minimal code to pass
- [ ] Step 7: Verify coverage (happy path, errors, edge cases)Test Writing Progress:
- [ ] Step 1: Review project standards (check existing tests)
- [ ] Step 2: Understand behavior (what should it do? what can fail?)
- [ ] Step 3: Choose test type (Integration/E2E/Unit)
- [ ] Step 4: Identify dependencies (real vs mocked)
- [ ] Step 5: Write failing test first (TDD)
- [ ] Step 6: Implement minimal code to pass
- [ ] Step 7: Verify coverage (happy path, errors, edge cases)Is this a complete user workflow?
→ YES: E2E test
Is this a pure function (no side effects/dependencies)?
→ YES: Unit test
Everything else:
→ Integration test (with real dependencies)Is this a complete user workflow?
→ YES: E2E test
Is this a pure function (no side effects/dependencies)?
→ YES: Unit test
Everything else:
→ Integration test (with real dependencies)describe("Feature Name", () => {
setup(initialState)
test("should produce expected output when action is performed", () => {
// Arrange: Set up preconditions
// Act: Perform the action being tested
// Assert: Verify observable output
})
})describe("Feature Name", () => {
setup(initialState)
test("should produce expected output when action is performed", () => {
// Arrange: Set up preconditions
// Act: Perform the action being tested
// Assert: Verify observable output
})
})// BAD: Guessing at timing
sleep(500)
assert result == expected
// GOOD: Wait for the actual condition
wait_for(lambda: result == expected)sleepsetTimeoutSkill(ce:condition-based-waiting)// BAD: Guessing at timing
sleep(500)
assert result == expected
// GOOD: Wait for the actual condition
wait_for(lambda: result == expected)sleepsetTimeoutSkill(ce:condition-based-waiting)| Context | Assert On | Avoid |
|---|---|---|
| UI | Visible text, accessibility roles, user-visible state | CSS classes, internal state, test IDs |
| API | Response body, status code, headers | Internal DB state directly |
| CLI | stdout/stderr, exit code | Internal variables |
| Library | Return values, documented side effects | Private methods, internal state |
| 场景 | 应断言的内容 | 应避免的内容 |
|---|---|---|
| UI | 可见文本、无障碍角色、用户可见状态 | CSS类、内部状态、测试ID |
| API | 响应体、状态码、请求头 | 直接断言内部数据库状态 |
| CLI | stdout/stderr、退出码 | 内部变量 |
| 库 | 返回值、文档化的副作用 | 私有方法、内部状态 |
// Good - References actual constant or fixture
expected_message = APP_MESSAGES.SUCCESS
assert response.message == expected_message
// Bad - Hard-coded, breaks when copy changes
assert response.message == "Action completed successfully!"// Good - References actual constant or fixture
expected_message = APP_MESSAGES.SUCCESS
assert response.message == expected_message
// Bad - Hard-coded, breaks when copy changes
assert response.message == "Action completed successfully!"// BAD: Testing that the mock was called, not real behavior
mock_service.assert_called_once()
// GOOD: Test the actual outcome
assert user.is_active == True
assert len(sent_emails) == 1// BAD: Testing that the mock was called, not real behavior
mock_service.assert_called_once()
// GOOD: Test the actual outcome
assert user.is_active == True
assert len(sent_emails) == 1// BAD: destroy() only used in tests - pollutes production code
class Session:
def destroy(self): # Only exists for test cleanup
...
// GOOD: Test utilities handle cleanup// BAD: destroy() only used in tests - pollutes production code
class Session:
def destroy(self): # Only exists for test cleanup
...
// GOOD: Test utilities handle cleanup
**Gate:** Before adding methods to production code, ask "Is this only for tests?" Yes → Put in test utilities.
**检查点:** 在为生产代码添加方法之前,自问「这个方法仅用于测试吗?」如果是 → 移至测试工具类中。// BAD: Mock prevents side effect test actually needs
mock(database.save) # Now duplicate detection won't work!
add_item(item)
add_item(item) # Should fail as duplicate, but won't
// GOOD: Mock at correct level
mock(external_api.validate) # Mock slow external call only
add_item(item) # DB save works, duplicate detected
add_item(item) # Fails correctly// BAD: Mock prevents side effect test actually needs
mock(database.save) # Now duplicate detection won't work!
add_item(item)
add_item(item) # Should fail as duplicate, but won't
// GOOD: Mock at correct level
mock(external_api.validate) # Mock slow external call only
add_item(item) # DB save works, duplicate detected
add_item(item) # Fails correctly// BAD: Partial mock - missing fields downstream code needs
mock_response = {
status: "success",
data: {...}
// Missing: metadata.request_id that downstream code uses
}
// GOOD: Mirror real API completely
mock_response = {
status: "success",
data: {...},
metadata: {request_id: "...", timestamp: ...}
}// BAD: Partial mock - missing fields downstream code needs
mock_response = {
status: "success",
data: {...}
// Missing: metadata.request_id that downstream code uses
}
// GOOD: Mirror real API completely
mock_response = {
status: "success",
data: {...},
metadata: {request_id: "...", timestamp: ...}
}references/javascript-react.mdreferences/python.mdreferences/go.mdreferences/javascript-react.mdreferences/python.mdreferences/go.md| Test Type | When | Dependencies |
|---|---|---|
| Integration | Default choice | Real (test DB, real modules) |
| E2E | Critical user workflows | Real (full stack) |
| Unit | Pure functions only | None |
| Anti-Pattern | Fix |
|---|---|
| Testing mock existence | Test actual outcome instead |
| Test-only methods in production | Move to test utilities |
| Mocking without understanding | Understand dependencies, mock minimally |
| Incomplete mocks | Mirror real API completely |
| Tests as afterthought | TDD - write tests first |
| Arbitrary timeouts/sleeps | Use condition-based waiting |
| 测试类型 | 适用场景 | 依赖项 |
|---|---|---|
| 集成测试 | 默认选择 | 真实依赖(测试数据库、真实模块) |
| E2E测试 | 关键用户工作流 | 真实依赖(全栈) |
| 单元测试 | 仅纯函数 | 无依赖项 |
| 反模式 | 修复方案 |
|---|---|
| 测试模拟对象的存在 | 改为测试实际结果 |
| 生产代码中的仅测试方法 | 移至测试工具类 |
| 未理解依赖就进行模拟 | 理解依赖项,最小化模拟 |
| 不完整的模拟对象 | 完全镜像真实API |
| 测试作为事后补充 | TDD - 先编写测试 |
| 任意超时/sleep | 使用基于条件的等待 |