IOS XCTest使用异步测试
来源:互联网 发布:微机室上网控制软件 编辑:程序博客网 时间:2024/09/21 06:35
XCTest使用异步测试需要用到XCTestExpectation
这个类,
- 首先在测试方法中创建一个
XCTestExpectation
对象expectation
。
XCTestExpectation* exception = [self expectationWithDescription:@"xx"];
- 然后执行自定义的异步方法。在这里测试使用
dispatch_async
执行异步操作,真实的测试环境可能是执行一个异步的网络请求,在异步任务执行完成之后需要调用XCTestExpectation
对象expectation
的fullfill
方法,网络请求中需要再网络请求完成之后调用该方法。
dispatch_queue_t queue = dispatch_queue_create("group.queue", DISPATCH_QUEUE_SERIAL); dispatch_block_t block = dispatch_block_create(0, ^{ [NSThread sleepForTimeInterval:1.0f]; printf("=====block invoke=====\n"); [expectation fulfill]; }); dispatch_async(queue, block);
- 调用
waitForExpectationsWithTimeout:handler
方法传递一个时间参数和超时处理的block。
[self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) { }];
完整的代码
- (void)testAsync { XCTestExpectation* expectation = [self expectationWithDescription:@"xx"]; dispatch_queue_t queue = dispatch_queue_create("group.queue", DISPATCH_QUEUE_SERIAL); dispatch_block_t block = dispatch_block_create(0, ^{ [NSThread sleepForTimeInterval:1.0f]; printf("=====block invoke=====\n"); [expectation fulfill]; }); dispatch_async(queue, block); [self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) { }];}
阅读全文
0 0
- IOS XCTest使用异步测试
- ios测试-使用XCTest进行单元测试
- iOS输入法—基于XCTest框架的异步测试
- iOS测试-XCTest
- IOS测试:XCTest小试牛刀
- ios测试-(一)使用XCTest进行单元测试
- ios测试-(一)使用XCTest进行单元测试
- iOS --- 使用XCTest进行单元测试
- iOS 测试入门 XCTest入门(一)
- iOS XCTest
- 【iOS】应用XCTest对异步任务进行单元测试
- iOS 单元测试- Xcode 7测试工具XCTest学习
- XCTest 测试实战
- iOS 读书笔记-单元测试XCTest
- 初探iOS单元测试XCTest
- iOS 自动化单元测试--XCTest
- iOS 自动化单元测试--XCTest
- 使用xctest私有库
- Spring 框架学习(三):IoC 容器
- NOIP模拟 探险 【树形dp】
- IOS分享扩展使用JS脚本
- xib和storyboard的不同点总结
- Visual Studio使用技巧
- IOS XCTest使用异步测试
- Kotlin传递可变长参数给Java可变参数方法
- PHP错误日志记录:display_errors与log_errors的区别
- hash-mysql索引
- UIWebView中Objective-C和JavaScript通信
- Linux 基础命令(五)—— 文本处理三剑客之grep
- WKWebView OC和JS交互
- memcached可视化客户端工具
- UIView 的 weak 和 strong 修饰符使用的场景