iOS -关于Xcode中的单元测试unitTest的使用

来源:互联网 发布:说说人工智能喜马拉雅 编辑:程序博客网 时间:2024/04/29 21:52

不多说,直接说方法步骤。

首先新建一个工程作为单元测试的程序。


图片中的Include Unit Tests一定要记得勾选

但如果忘记勾选,可以有其他办法创建:

File-->new-->target-->iOS-->iOS Unit Testing Bundle。

工程创建完毕,来到这个蓝色选中的地方。


然后解释一下这个类中的几个方法都是什么意思:

- (void)setUp {    [super setUp];    //初始化的代码,在测试方法调用之前调用    // Put setup code here. This method is called before the invocation of each test method in the class.}- (void)tearDown {    // Put teardown code here. This method is called after the invocation of each test method in the class.    // 释放测试用例的资源代码,这个方法会每个测试用例执行后调用    [super tearDown];}- (void)testExample {    // This is an example of a functional test case.    // Use XCTAssert and related functions to verify your tests produce the correct results.    // 测试用例的例子,注意测试用例一定要test开头}- (void)testPerformanceExample {    // 测试性能例子    // This is an example of a performance test case.    [self measureBlock:^{        // 需要测试性能的代码        // Put the code you want to measure the time of here.    }];}

在ViewController.m中写方法

- (int)getNum{        return 123;}

然后来到测试里。

现在import一下ViewController控制器,然后声明一个vc的属性

@property (nonatomic,strong) ViewController *vc;

然后按照如下操作:

- (void)setUp {    [super setUp];    //初始化的代码,在测试方法调用之前调用    // Put setup code here. This method is called before the invocation of each test method in the class.    // 实例化    self.vc = [[ViewController alloc] init];}- (void)tearDown {        // 清空    self.vc = nil;    // Put teardown code here. This method is called after the invocation of each test method in the class.    // 释放测试用例的资源代码,这个方法会每个测试用例执行后调用    [super tearDown];}- (void)testExample {    // This is an example of a functional test case.    // Use XCTAssert and related functions to verify your tests produce the correct results.    // 测试用例的例子,注意测试用例一定要test开头        // 调用测试方法    int result = [self.vc getNum];        // 测试是否相等    XCTAssertEqual(result, 123,@"测试不通过");}- (void)testPerformanceExample {    // 测试性能例子    // This is an example of a performance test case.    [self measureBlock:^{        // 需要测试性能的代码        // Put the code you want to measure the time of here.    }];}

通过
command + U进行编译,如果旁边显示绿色勾勾,表示测试通过。

如果此时改变数值,将123修改为其他数值,则会报红色叉叉


附上一些测试用例的方法:

 XCTFail(format…) 生成一个失败的测试; XCTAssertNil(a1, format...)为空判断,a1为空时通过,反之不通过; XCTAssertNotNil(a1, format…)不为空判断,a1不为空时通过,反之不通过; XCTAssert(expression, format...)当expression求值为TRUE时通过; XCTAssertTrue(expression, format...)当expression求值为TRUE时通过; XCTAssertFalse(expression, format...)当expression求值为False时通过; XCTAssertEqualObjects(a1, a2, format...)判断相等,[a1 isEqual:a2]值为TRUE时通过,其中一个不为空时,不通过; XCTAssertNotEqualObjects(a1, a2, format...)判断不等,[a1 isEqual:a2]值为False时通过; XCTAssertEqual(a1, a2, format...)判断相等(当a1和a2是 C语言标量、结构体或联合体时使用, 判断的是变量的地址,如果地址相同则返回TRUE,否则返回NO); XCTAssertNotEqual(a1, a2, format...)判断不等(当a1和a2是 C语言标量、结构体或联合体时使用); XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判断相等,(double或float类型)提供一个误差范围,当在误差范围(+/-accuracy)以内相等时通过测试; XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判断不等,(double或float类型)提供一个误差范围,当在误差范围以内不等时通过测试; XCTAssertThrows(expression, format...)异常测试,当expression发生异常时通过;反之不通过;(很变态) XCTAssertThrowsSpecific(expression, specificException, format...) 异常测试,当expression发生specificException异常时通过;反之发生其他异常或不发生异常均不通过; XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression发生具体异常、具体异常名称的异常时通过测试,反之不通过; XCTAssertNoThrow(expression, format…)异常测试,当expression没有发生异常时通过测试; XCTAssertNoThrowSpecific(expression, specificException, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过; XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过


完。




0 0
原创粉丝点击