码迷,mamicode.com
首页 > 其他好文 > 详细

XCTest的小技巧

时间:2015-07-01 18:30:18      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:xcode   unit-testing   xctest   单元测试   ios   

XCTest 的小技巧

试了一下在一个测试里,以不同的状态把其它测试方法跑一遍。

    // alternate what setup done

    // full tests in alternated state
    NSArray * tests = [FileLibraryTests testInvocations];
    for (NSInvocation * inv in tests ) {
        NSString * sel = NSStringFromSelector(inv.selector);
        if (![sel containsString:@"WithInit"] && ![sel containsString:@"Monitoring"]) {
            [inv invokeWithTarget:self];
        }
    }

    //back to normal state

另外,Xcode 6 已经加上了 XCTestExpetation,以后用不着:

    #import <XCTest/XCTestCase+AsynchronousTesting.h>

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    dispatch_after(dispatch_time(0, (int64_t)(0.2 * NSEC_PER_SEC)), 
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
        ^{
            ... dispatch_semaphore_signal(sema); ...
        }
    );

    dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, 30*NSEC_PER_SEC));

直接写

    XCTestExpectation *expectation = [self expectationWithDescription:@"xxx"];

    // invoke it in code async executing
    ... ^(){
        [expectation fulfill];
    } ...

    [self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
        // cleanup.
    }];

版权声明:本文为博主原创文章,未经博主允许不得转载。

XCTest的小技巧

标签:xcode   unit-testing   xctest   单元测试   ios   

原文地址:http://blog.csdn.net/pinxue/article/details/46711063

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!