(transfer) [ios] unit test

 Reprinted from: http://www.jianshu.com/p/009844a0b9ed

 

![Uploading QQ20160129-3_262826.png . . .]####What is unit testing?
When I hear the word unit testing, it feels very high-end. In fact, unit testing is to write a test function for your method. To ensure that your method is constantly being modified and developed. keep it right. If there is an error, let you know as soon as possible, so that monitoring from the smallest unit to ensure the quality of the software.

When to use unit testing:

1. After writing the code: I want to verify whether there is any problem with the code I wrote.
2. Before writing code: that is, before writing code, all functional sub-modules are designed, and the test is passed before writing. (I haven't used it anyway).
3. After fixing a bug: Generally, after fixing a certain bug, in order to ensure that the repair is successful, a test will be written.

How to write unit tests

It seems that there is a lot of nonsense, so let's go straight to the topic.
Create a project, choose any name, and check include Unit Tests directly


QQ20160129-0.png


What if I forget to tick it? There can be other ways to create File-->new-->target-->iOS-->iOS Unit Testing Bundle. Make up your own name.




QQ20160129-1.png

 

After the project is created, how to start testing?
Find the .m file in the Testes folder of the system unit test and you will see several methods. Let's take a look at when these methods are called and their various functions.


QQ20160129-2.png
  - (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.
// 需要测试性能的代码
}];
}

Write a simple method in ViewController

- (int)getNum;

accomplish:

- (int)getNum {

return 100;
}

Import ViewController.h in the test file and define a vc property

 #import <XCTest/XCTest.h>

#import "ViewController.h"

@interface ____Tests : XCTestCase

@property (nonatomic,strong) ViewController *vc;


@end

@implementation ____Tests

Implementation of test cases

- (void)setUp {
[super setUp];

// 实例化需要测试的类
self.vc = [[ViewController alloc] init];
}

- (void)tearDown {
// 清空
self.vc = nil;

[super tearDown];
}

- (void)testMyFuc {

// 调用需要测试的方法,
int result = [self.vc getNum];
// 如果不相等则会提示@“测试不通过”
XCTAssertEqual(result, 100,@"测试不通过");
}

The command+u shortcut runs, or produce-->test, and the
project runs.


QQ20160129-3.png


我们可以在在控制台清晰的看到我们要测试的用例子通过了,测试通过的测试方法会有绿色的钩。

这时候我们改下断言,把100随便改成一个数,120.再comand+u运行下,看下什么情况

QQ20160129-4.png

很明显是能不能通过的,因为我们要测试的方法返回值是100,

自带的测试框架还能测试某个方法的性能,

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
    // Put the code you want to measure the time of here.

    for (int i = 0; i<100; i++) {

        NSLog(@"dd");
    }
}];

}

我们在例子中添加一个for循环,测试其性能。command+u运行就能看到如图:


QQ20160129-5.png

能够非常直观的看出其调用的时间,可以用其来对比性能的优劣。

另外XCTest还支持异步单元测试,我就不在这里展开了。最后附上常用的断言及解释。

  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没有发生具体异常、具体异常名称的异常时通过测试,反之不通过

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397326&siteId=291194637