iOS -关于Xcode中的单元测试unitTest的使用
2016-06-12 17:02
429 查看
不多说,直接说方法步骤。
首先新建一个工程作为单元测试的程序。
图片中的Include Unit Tests一定要记得勾选。
但如果忘记勾选,可以有其他办法创建:
File-->new-->target-->iOS-->iOS
Unit Testing Bundle。
工程创建完毕,来到这个蓝色选中的地方。
然后解释一下这个类中的几个方法都是什么意思:
在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没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
完。
首先新建一个工程作为单元测试的程序。
图片中的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没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
完。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Ruby单元测试框架TestUnit的替代者MiniTest介绍
- Android学习笔记之应用单元测试实例分析
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法