覆盖率报告 Xcode7开始,自带覆盖率
设置要点:
-1)Scheme中开启Gather Coverage data
-2) 针对于AppTarget来测试,而非Test Target
框架
-expecta expect(error).not.beNil()
-specter describe(“”)it(“”)
-Kiwi describe(“”)it(“”)
执行测试快捷键
cmd + 5 切换到测试选项卡后会看到很多小箭头,点击可以单独或整体测试
cmd + U 运行整个单元测试
一.方法注解
// 在每一个测试用例开始前调用,用来初始化相关数据
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invo cation of each test method in the class.
//初始化的代码,在测试方法调用之前调用
}
// 在测试用例完成后调用,可以用来释放变量等结尾操作
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// 释放测试用例的资源代码,这个方法会每个测试用例执行后调用
[super tearDown];
}
// 测试方法
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// 测试用例的例子,注意测试用例一定要test开头
}
// 性能测试方法,通过测试block中方法执行的时间,比对设定的标准值和偏差觉得是否可以通过测试
- (void)testPerformanceExample {
// This is an example of a performance test case.
// 测试性能例子,有Instrument调试工具之后,其实这个没毛用。
[self measureBlock:^{
// Put the code you want to measure the time of here.
// 需要测试性能的代码
}];
}
二.网络请求方法测试
-(void)testRequest{
XCTestExpectation *expectation =[self expectationWithDescription:@"没有满足期望"];
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
[sessionManager GET:@"http://www.weather.com.cn/adat/sk/101110101.html" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject:%@", [NSJSONSerialization JSONObjectWithData:responseObject options:1 error:nil]);
XCTAssertNotNil(responseObject, @"返回出错");
[expectation fulfill];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
XCTAssertNil(error, @"请求出错");
}];
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
if (error) {
NSLog(@"Timeout Error: %@", error);
}}];}
三.模拟异步测试
- (void)testExample {
XCTestExpectation *exp = [self expectationWithDescription:@"这里可以是操作出错的原因描述。。。"];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
//模拟这个异步操作需要2秒后才能获取结果,比如一个异步网络请求
sleep(2);
//模拟获取的异步操作后,获取结果,判断异步方法的结果是否正确
XCTAssertEqual(@"a", @"a");
//如果断言没问题,就调用fulfill宣布测试满足
[exp fulfill];
}];
//设置延迟多少秒后,如果没有满足测试条件就报错
[self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"Timeout Error: %@", error);
}}];}

网友评论