cocoajin

xcode集成了非常方便的测试框架,XCTest

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

 

分类:

技术点:

相关文章:

  • 2022-01-16
  • 2021-06-15
  • 2021-05-17
  • 2021-08-10
  • 2021-08-22
  • 2021-10-06
  • 2021-05-08
  • 2021-11-05
猜你喜欢
  • 2019-01-24
  • 2021-10-06
  • 2021-12-20
  • 2021-11-27
  • 2021-05-05
  • 2018-01-11
  • 2021-04-24
相关资源
相似解决方案