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); } }]; }