iOS测试

来源:互联网 发布:国泰安经济金融数据库 编辑:程序博客网 时间:2024/05/21 22:34

文件一:ViewControllerTest.m

#import <UIKit/UIKit.h>

#import <XCTest/XCTest.h>

#import "ViewController.h"

@interface ViewControllerTest : XCTestCase
{
    ViewController *_viewCtrl;
}
@end

@implementation ViewControllerTest

- (void)setUp {
    [super setUp];
    
    _viewCtrl = [[ViewController alloc] init];
}

- (void)tearDown {
    _viewCtrl = nil;
    
    [super tearDown];
}

- (void)testRandomNumber {
    NSInteger result1 = [_viewCtrl randomNumber];
    NSInteger result2 = [_viewCtrl randomNumber];
    XCTAssert(result1 != result2, @"随机数");
}

- (void)testRandomLessThan100 {
    NSInteger result = [_viewCtrl randomNumber];
    XCTAssert(result < 100, @"小于100");
}

- (void)testFile {
    NSBundle *bundle = [NSBundle bundleForClass:[ViewControllerTest class]];
    NSString *path = [bundle pathForResource:@"test" ofType:@"js"];
    XCTAssert([path hasSuffix:@"test.js"]);
}

//异步测试
- (void)testDowload {
    XCTestExpectation *expectation = [self expectationWithDescription:@"xxxx"];
    
    [_viewCtrl print:^{
        [expectation fulfill];
    }];
    
    [self waitForExpectationsWithTimeout:6 handler:nil];
}

- (void)testDraw {
    [self measureBlock:^{
        [_viewCtrl draw];
    }];
}

@end


文件二:ViewContorller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (NSInteger)randomNumber;
- (void)print:(void (^)(void))block;
- (void)draw;
@end


文件三:ViewContorller.m

#import "ViewController.h"

@interface ViewController ()
{
    void (^_block)(void);
}
@end

@implementation ViewController

- (NSInteger)randomNumber
{
    int num = arc4random() % 100;
    return num;
}

- (void)print:(void (^)(void))block
{
    _block = block;
    [self performSelector:@selector(display) withObject:nil afterDelay:1];
}

- (void)display
{
    _block();
}

- (void)draw
{
    for (int i = 0; i < 10000; i++) {
//        NSLog(@"%d", i);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

0 0
原创粉丝点击