Sloth单元测试策略:从模块测试到集成验证
Sloth作为基于lsof命令的macOS图形化工具,其测试策略需覆盖Objective-C/Cocoa架构下的模块独立性与系统集成性。项目核心功能通过source/SlothController.m协调进程扫描、数据解析与UI展示,测试需围绕这一主线构建分层验证体系。
模块测试框架设计
工具类测试方案
针对source/Util/中的通用工具,采用XCTest单元测试框架实现独立验证:
IP地址解析测试
source/Util/IPUtils.h提供网络地址处理功能,测试用例应覆盖:
- (void)testIPV4Parsing {
NSString *input = @"192.168.1.1:8080";
NSDictionary *result = [IPUtils parseIPString:input];
XCTAssertEqualObjects(result[@"address"], @"192.168.1.1");
XCTAssertEqual([result[@"port"] integerValue], 8080);
}
文件系统工具测试
source/Util/FSUtils.h需验证路径规范化与权限检查:
- (void)testPathResolution {
NSString *path = [FSUtils resolveSymlink:@"/var/log"];
XCTAssertTrue([path isEqualToString:@"/private/var/log"]);
}
业务逻辑测试重点
source/LsofTask.h作为解析lsof输出的核心模块,需构建模拟命令输出进行测试:
- (void)testLsofParsing {
NSString *sampleOutput = @"p1234\nf5\ntREG\nn/Users/test/file.txt";
LsofTask *task = [[LsofTask alloc] init];
NSArray<Item *> *items = [task parseOutput:sampleOutput];
XCTAssertEqual(items.count, 1);
Item *item = items.firstObject;
XCTAssertEqual(item.pid, 1234);
XCTAssertEqual(item.fileDescriptor, 5);
XCTAssertEqual(item.type, FileTypeRegular);
}
组件交互测试设计
控制器协作测试
source/SlothController.h与source/InfoPanelController.h的交互需通过XCTestExpectation验证异步更新:
- (void)testInfoPanelUpdate {
SlothController *controller = [[SlothController alloc] init];
InfoPanelController *panel = [[InfoPanelController alloc] init];
controller.infoPanel = panel;
Item *testItem = [[Item alloc] init];
testItem.path = @"/tmp/test";
XCTestExpectation *expectation = [self expectationWithDescription:@"Panel update"];
dispatch_async(dispatch_get_main_queue(), ^{
[controller showInfoForItem:testItem];
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:1 handler:nil];
XCTAssertEqualObjects(panel.pathField.stringValue, @"/tmp/test");
}
数据模型验证
source/Item.h作为核心数据模型,需验证KVO合规性:
- (void)testItemKVO {
Item *item = [[Item alloc] init];
item.pid = 123;
__block BOOL didChange = NO;
[item addObserver:self forKeyPath:@"pid" options:NSKeyValueObservingOptionNew context:nil];
item.pid = 456;
didChange = YES;
XCTAssertTrue(didChange);
[item removeObserver:self forKeyPath:@"pid"];
}
集成测试策略
端到端流程验证
通过模拟用户操作链验证完整功能:
- 启动扫描([SlothController scan:]方法)
- 过滤进程([SlothController filterContentForSearchString:])
- 查看详情([InfoPanelController setItem:])
- (void)testFullWorkflow {
SlothController *controller = [[SlothController alloc] init];
// 模拟扫描完成
[controller scanDidFinishWithItems:[self generateTestItems]];
// 验证UI更新
XCTAssertEqual(controller.outlineView.numberOfRows, 5);
// 测试过滤
[controller filterContentForSearchString:@"Terminal"];
XCTAssertEqual(controller.outlineView.numberOfRows, 2);
}
系统集成测试
验证与lsof命令行工具的集成(Common.h中定义的LSOF_PATH常量):
- (void)testLsofIntegration {
NSString *lsofPath = LSOF_PATH; // 来自Common.h的常量定义
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:lsofPath]);
LsofTask *task = [[LsofTask alloc] init];
XCTestExpectation *expectation = [self expectationWithDescription:@"lsof execution"];
[task runWithCompletion:^(NSArray<Item *> *items, NSError *error) {
XCTAssertNil(error);
XCTAssertGreaterThan(items.count, 0);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
}
测试环境配置
测试宏定义
利用Common.h中的DEBUG宏实现条件测试代码:
#ifdef DEBUG
#define DLog(...) NSLog(__VA_ARGS__)
#else
#define DLog(...)
#endif
测试数据生成
创建测试专用工具类生成模拟数据:
@implementation TestDataGenerator
+ (NSArray<Item *> *)generateTestItems {
NSMutableArray *items = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
Item *item = [[Item alloc] init];
item.pid = 1000 + i;
item.path = [NSString stringWithFormat:@"/test/path%d.txt", i];
[items addObject:item];
}
return items;
}
@end
测试覆盖率目标
| 模块 | 目标覆盖率 | 测试类型 |
|---|---|---|
| Util工具类 | ≥90% | 单元测试 |
| LsofTask解析 | ≥85% | 单元+集成测试 |
| SlothController | ≥70% | 组件测试 |
| Item模型 | ≥95% | 单元测试 |
注:覆盖率统计需通过Xcode的Test Navigator启用"Show Code Coverage"功能
持续集成配置
在Makefile中添加测试目标(Makefile):
test:
xcodebuild test -project Sloth.xcodeproj -scheme Sloth -destination 'platform=macOS'
通过GitHub Actions配置自动测试:
jobs:
test:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- run: make test
测试策略演进建议
- 添加UI测试:使用XCTest UI录制功能验证用户交互流程
- 性能测试:针对LsofTask.m解析性能添加基准测试
- 安全测试:验证STPrivilegedTask.h的权限处理
建议定期审查TODO.md中的测试相关条目,优先实现标记为"Critical"的测试用例。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



