一键导入
test-driven-development
Use when implementing or modifying core SDK paths (event pipeline, storage, network, session management)
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when implementing or modifying core SDK paths (event pipeline, storage, network, session management)
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when receiving an ambiguous feature request, when scope is unclear, or before writing any plan
Use after verification-before-completion passes and code review is clean, to close out the development branch
Use after writing a plan document in docs/plans/, before requesting user confirmation
Use when receiving feedback from code-reviewer subagent or human reviewer
Use when a feature, bugfix, or refactoring step is completed and needs review, or before merging to main, or when user says "review", "审查", "帮我看看代码"
Use when executing an implementation plan with 3+ independent tasks in the current session
| name | test-driven-development |
| description | Use when implementing or modifying core SDK paths (event pipeline, storage, network, session management) |
Type: Technique | Discipline: Rigid
在 SDK 核心路径上,先写会失败的测试,再写通过测试的最小实现。
核心原则: 测试先行 = 接口先行。想不出怎么测,通常是接口设计有问题。
循环节奏: 红 → 绿 → 重构,单次循环 ≤ 10 分钟。
GrowingBaseEvent)、事件持久化、事件发送GrowingEventDatabase(SQLite 读写)Example/GrowingAnalyticsTests/
├── TrackerCoreTests/ ← CoreTests / DatabaseTests / DeepLinkTests /
│ EventTests / FileStorageTests / HelpersTests /
│ HookTests / ManagerTests / MenuTests /
│ SwizzleTests / ThreadTests / UtilsTests
├── AutotrackerCoreTests/ ← Autotrack / GrowingNodeTests
├── ModulesTests/ ← ABTestingTests / AdvertisingTests / HybridTests /
│ MobileDebuggerTests / ProtobufTests / WebCircleTests
├── ServicesTests/
├── HostApplicationTests/
├── GrowingAnalyticsStartTests/
├── GrowingAnalyticsUITests/
└── Helper/ ← 复用的 Mock/Invocation/ManualTrack helper
所有测试源码最终都编译进 GrowingAnalyticsTests 这个 Xcode target(不是每个子目录一个 target)。运行时按 GrowingAnalyticsTests.xctestplan 调度,靠 -only-testing: 过滤子集。
@interface GrowingEventDatabaseTest : XCTestCase
@end
@implementation GrowingEventDatabaseTest
- (void)setUp {
[super setUp];
// 每个 case 前重置状态
}
- (void)tearDown {
// 清理副作用,重置单例
[super tearDown];
}
- (void)test_shouldPersistEventAfterTrack {
// Red → Green → Refactor
XCTAssertEqual(count, expectedCount);
}
@end
| 断言 | 用途 |
|---|---|
XCTAssertEqual(a, b) | 相等 |
XCTAssertNil(x) / XCTAssertNotNil(x) | 空值 |
XCTAssertTrue(x) / XCTAssertFalse(x) | 布尔 |
XCTAssertThrows(expr) | 异常 |
XCTAssertEqualObjects(a, b) | 对象相等 |
# 与 .github/workflows/ci.yml 保持一致
xcodebuild test \
-workspace Example/GrowingAnalytics.xcworkspace \
-scheme GrowingAnalyticsTests \
-testPlan GrowingAnalyticsTests \
-destination 'platform=iOS Simulator,name=iPhone 17 Pro' \
| xcbeautify
# 只跑单个子集(举例)
xcodebuild test \
-workspace Example/GrowingAnalytics.xcworkspace \
-scheme GrowingAnalyticsTests \
-destination 'platform=iOS Simulator,name=iPhone 17 Pro' \
-only-testing:GrowingAnalyticsTests/GrowingEventDatabaseTest \
| xcbeautify
| xcbeautify是 CI 当前使用的格式化工具(xcpretty亦可,但项目 workflow 已统一到 xcbeautify)。设备名以本机xcrun simctl list devices available为准。
事件从 track: 调用到入库是异步链路(后台 GCD 队列)。测试时:
XCTestExpectation + waitForExpectations:tearDown 必须重置会被当前 case 污染到的全局状态:SDK 单例、事件数据库、配置缓存等。SDK 本身不暴露统一的 reset API,具体策略看对象类型:
[GrowingHybridModule.sharedInstance resetBridgeSettings]Example/GrowingAnalyticsTests/Helper/ 下测试辅助创建的数据库文件GrowingConfigurationManager 重新 setConfigurationMockEventQueue、InvocationHelper- (void)tearDown {
// 按本 case 涉及的副作用逐个还原;禁止留空 tearDown
[super tearDown];
}
任何采集路径都必须有"关闭总开关后不采集"的测试。
| Excuse | Reality |
|---|---|
| "先写实现再补测试" | 测试退化为"验证代码写了什么" |
| "这块改动太简单不用测试" | 核心路径零例外 |
| "测试第一次就绿了" | 没见过 Red 的 Green 不可信 |
| "dataCollectionEnabled=false 的路径不用测" | 这是隐私合规红线 |
verification-before-completion