测试用例开始之前自定义初始状态。
- (void)setUp {
self.vc = [[ViewController alloc] init];
}
测试用例结束后提供执行清除。
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
self.vc = nil;
}
逻辑测试
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// 10 + 20 = 30 = 20 ?
// 测试三部曲
// give -- 我们给预期
int num1 = 10;
int num2 = 20;
// when
int num3 = [self.vc getPlus:num1 num2:num2];
// then
XCTAssertEqual(num3, 30,@"没有达到预想结果");
}
异步测试 - test开头
- (void)testAsy{
// give
XCTestExpectation *ec = [self expectationWithDescription:@"你是好人"];
// when
[self.vc loadData:^(id data) {
// give
// when - 逻辑
XCTAssertNotNil(data);
[ec fulfill]; //完成填充
}];
// then
[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
NSLog(@"error = %@",error);
}];
}
性能测试+异步测试
// 性能只有相对性 -- 1s
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// 测试性能
// 总时间
// 平均时间
// 散列分布
// 数学统计学
// 不做测试考虑范围
[self.vcß openCamera];//提供条件
[self.vc openCamera];//局部测试
}];
}
局部性能测试
- (void)testPerformance{
[self measureMetrics:@[XCTPerformanceMetric_WallClockTime] automaticallyStartMeasuring:NO forBlock:^{
[self.vc openCamera];//提供条件
[self startMeasuring];
[self.vc openCamera];//局部测试
[self stopMeasuring];
}];
}
网友评论