iOS-使用Xcode自带单元测试UnitTest

转自://www.greatytc.com/p/009844a0b9ed

什么是单元测试?
一听到单元测试这个词感觉很高端,其实单元测试就是为你的方法多专门写一个测试函数。以保证你的方法在不停的修改开发中。保持正确。如果出错,第一时间让你知道,这样从最小单位开始监控来保证软件的质量。

什么时候用到单元测试:
1、写完代码以后:想要验证一下自己写的代码是否有问题。
2、写代码之前:就是写代码之前所有的功能分模块的设计好,测试通过了再写。(我反正是没用过)。
3、修复某个bug后:一般修复完某个bug,为了确保修复是成功的,会写测试。

怎么写单元测试
好像废话有点多了,还是直接奔主题吧。
创建一个工程,名字随便取,直接勾选include Unit Tests

2D0F76DC-47AE-4429-92FF-EA4B632367EA.png

万一我忘了勾选怎么办呢?可以有其他方式创建File-->new-->target-->iOS-->iOS Unit Testing Bundle。名字自己看着办吧。


970305-4cb898257beda52d.png
970305-4904c381bf62da67.png

工程创建好后,那要怎么开始测试呢?
找到系统单元测试Testes文件夹中.m文件看中会到看到几个方法,我们来看下这个几个方法是什么时候调用和他们各种的作用


970305-d1f674bf0e438f0d.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.
    // 需要测试性能的代码
    }];
    }

在ViewController中写一个简单的方法

  • (int)getNum;

实现:

  • (int)getNum {return 100;}

在测试的文件中导入ViewController.h,并且定义一个vc属性

import <XCTest/XCTest.h>#import "ViewController.h"@interface ____Tests : XCTestCase@property (nonatomic,strong) ViewController *vc;@end@implementation ____Tests

测试用例的实现

  • (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都行,工程就跑起来了

970305-f260bd130aaedfeb.png

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

970305-d515866fb648e86e.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运行就能看到如图:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容