![Uploading QQ20160129-3_262826.png . . .]####什么是单元测试?
一听到单元测试这个词感受很高端,其实单元测试就是为你的方法多专门写一个测试函数。以保证你的方法在不停的修改开发中。保持正确。若是出错,第一时间让你知道,这样从最小单位开始监控来保证软件的质量。express
一、写完代码之后:想要验证一下本身写的代码是否有问题。
二、写代码以前:就是写代码以前全部的功能分模块的设计好,测试经过了再写。(我反正是没用过)。
三、修复某个bug后:通常修复完某个bug,为了确保修复是成功的,会写测试。框架
好像废话有点多了,仍是直接奔主题吧。
建立一个工程,名字随便取,直接勾选include Unit Tests异步
万一我忘了勾选怎么办呢?能够有其余方式建立File-->new-->target-->iOS-->iOS Unit Testing Bundle。名字本身看着办吧。函数
工程建立好后,那要怎么开始测试呢?
找到系统单元测试Testes文件夹中.m文件看中会到看到几个方法,咱们来看下这个几个方法是何时调用和他们各类的做用性能
- (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中写一个简单的方法单元测试
- (int)getNum;
实现:测试
- (int)getNum { return 100; }
在测试的文件中导入ViewController.h,而且定义一个vc属性ui
#import <XCTest/XCTest.h> #import "ViewController.h" @interface ____Tests : XCTestCase @property (nonatomic,strong) ViewController *vc; @end @implementation ____Tests
测试用例的实现atom
- (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,@"测试不经过"); }
command+u快捷方式运行,或者produce-->test都行,
工程就跑起来了spa
咱们能够在在控制台清晰的看到咱们要测试的用例子经过了,测试经过的测试方法会有绿色的钩。
很明显是能不能经过的,由于咱们要测试的方法返回值是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运行就能看到如图:
可以很是直观的看出其调用的时间,能够用其来对比性能的优劣。
另外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没有发生具体异常、具体异常名称的异常时经过测试,反之不经过