美文网首页
IOS XCTest使用异步测试

IOS XCTest使用异步测试

作者: aron1992 | 来源:发表于2017-12-09 20:43 被阅读294次

XCTest使用异步测试需要用到XCTestExpectation这个类,

  1. 首先在测试方法中创建一个XCTestExpectation对象expectation
XCTestExpectation* exception = [self expectationWithDescription:@"xx"];
  1. 然后执行自定义的异步方法。在这里测试使用dispatch_async执行异步操作,真实的测试环境可能是执行一个异步的网络请求,在异步任务执行完成之后需要调用XCTestExpectation对象expectationfullfill方法,网络请求中需要再网络请求完成之后调用该方法。
    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);
  1. 调用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) {
        
    }];
}

相关文章

  • IOS XCTest使用异步测试

    XCTest使用异步测试需要用到XCTestExpectation这个类, 首先在测试方法中创建一个XCTestE...

  • 【开发技巧】单元测试

    iOS XCTest单元测试 iOS开发:XCTest单元测试(附上一个单例的测试代码) [iOS单元测试系列]单...

  • iOS单元测试

    iOS单元测试 前言 单元测试使用XCTest框架 单元测试的目标:模型文件 用处:不基于界面UI的情况下,保证模...

  • (二) kiwi 实践一二

      上一篇 初探 iOS 单元测试 我们简述了单元测试的目的和本质,并介绍了XCTest的常见用法。XCTest作...

  • iOS 如何进行单元测试 < 二 >

    iOS 单元测试和UI测试教程 - CocoaChina_让移动开发更简单 ObjC 中国 - XCTest 测试...

  • iOS测试框架XCTest

    XCTest已基本满足单元测试和UI测试的需求,支持对同步流程、异步流程、性能的测试,UI测试代码的自动生成,代码...

  • 使用Xcode测试及XCTest框架

    Xcode提供了XCTest框架用以编写测试代码。在创建Xcode工程时,Xcode默认使用XCTest,并且默认...

  • iOS UITest 自动化测试开发

    关于iOS的UI自动化测试,是从Xcode7之后才支持的比较好,使用XCTest.framework,Xcode可...

  • iOS自动化测试框架对比

    1. XCTest XCTest是苹果在iOS 7和Xcode5引入的一个简单而强大的测试框架,它的测试编写起来非...

  • IOS自动化工具

    1. XCTest XCTest是苹果在iOS 7和Xcode5引入的一个简单而强大的测试框架,它的测试编写起来非...

网友评论

      本文标题:IOS XCTest使用异步测试

      本文链接:https://www.haomeiwen.com/subject/zippixtx.html