xcode6 AsynchronousTesting 异步任务测试

xcode集成了很是方便的测试框架,XCTestxcode

在xcode6以后,提供了 <XCTest/XCTestCase+AsynchronousTesting.h>网络

利用此咱们能够直接在XCTest里面测试一些异步的任务,好比异步网络请求框架

以下示例异步

- (void)testExample {

    
    XCTestExpectation *exception = [self expectationWithDescription:@"TestException"];
    
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://httpbin.org/get"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        XCTAssertNil(connectionError,@"connectionError should nil");
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        [exception fulfill];
        
    }];
    
    [self waitForExpectationsWithTimeout:5.0f handler:nil];
}

另外 可使用 measureBlock 测试性能:性能

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        
        for (int i = 0; i < 10; i++) {
            NSLog(@"%d",i);
        }
    }];
}
相关文章
相关标签/搜索