iOS 读书笔记-单元测试XCTest

来源:互联网 发布:天宝erp软件 编辑:程序博客网 时间:2024/06/10 17:46

测试驱动下的iOS开发。这两天看了一下关于这方面的基础知识。扫一下盲。

测试驱动开发,英文Test-Driven Development 简称TDD。他是一种新型的软件开发方法。他要求在写代码之前先编写测试代码,然后之编写使测试通过的代码,通过测试来推动整个开发的进行。有助于编写简洁可用和高质量的代码。并加速开发过程。

传统开发:编写程序->设计测试用例->编写测试用例程序->单元测试->编写测试报告
测试驱动开发:设计单元测试用例->编写测试用例程序->程序编码->单元测试->编写测试报告

相比两种方法说白了就是,传统方法是写完工程代码,然后编写数据,在编写程序(或者手动)去测试,而测试驱动开发是先编写出测试数据和测试程序,然后再去编写工程代码。

据说好处大大的有。

单元测试是测试驱动的核心

主要的iOS单元测试框架有:OCUnit、GHUnit、XCTest。

OCUnit 、GHUnit都是开源的测试框架。
XCTest是Xcode默认的单元测试框架。

apple 一直致力于测试驱动开发,所以在每个工程中都默认添加了XCTest框架。

说实话以前这东西只是知道个名字,干什么的,怎么用都不知道。也没碰过。
来看下:
这里写图片描述
工程名后面加一个Tests的文件就是。里面包含一个.m文件和一个info.plist 文件。在TARGETS中也有他的身影。

那么我们怎么来使用它呢?

我们来做这么一个页面:
这里写图片描述
然后将Text Field控件连接viewController ,Button 的touch up Inside事件连接viewController 。

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *txtRevenue;- (IBAction)onClick:(id)sender;@end

然后我们来编写- (IBAction)onClick:(id)sender;方法

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];}- (IBAction)onClick:(id)sender {    _txtRevenue.text  = @"100";}@end

如果点击按钮我们让Text Field控件里面显示100;就这么简单。

这是我们写的功能。

那么我们要用单元测试来测试写的是否正确。
我们点开_Tests文件里面的.m文件里面是这样的

@interface PITaxTests : XCTestCase@end@implementation PITaxTests- (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.    XCTAssert(YES, @"Pass");}- (void)testPerformanceExample {    // This is an example of a performance test case.    [self measureBlock:^{        // Put the code you want to measure the time of here.    }];}
  • (void)setUp;方法是在测试开始是调用的方法,里面可初始化一些变量。
  • (void)tearDown ;方法是在测试完成之后调用的方法。
    另外两个方法是测试方法。

我们看到我们的测试类继承自XCTestCase,在运行时,XCTest框架会运行XCTestCase的子类中以test开头的测试方法(比如)testExample )。

那么我们来使用它进行测试吧。

我们就测试刚才写个功能,也就是点击界面上的计算按钮,然后再文本输入框中出现100这个功能。

看代码吧,一看就懂。

#import <UIKit/UIKit.h>#import <XCTest/XCTest.h>#import "ViewController.h"@interface PITaxTests : XCTestCase@property (nonatomic,strong) ViewController *viewController;@end@implementation PITaxTests- (void)setUp {    [super setUp];    UIApplication *application= [UIApplication sharedApplication] ;    UIWindow *window = [[application delegate] window];    _viewController =(ViewController *) window.rootViewController;}- (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.    XCTAssertNotNil(_viewController ,@"viewcontroller  is  nil");//这里是断言 下面说    [_viewController onClick:nil];    XCTAssertEqualObjects(_viewController.txtRevenue.text, @"100",@"100返回结果错误");//这里也是断言}@end

上面中的断言就是用来检验结果是否正确,如果错误会输出后面的提示信息。说明该测试用例错误。

只是记录一下学习单元测试,并没有按照测试驱动的方法来编写。
最后补充一下断言。
下面断言来自博客断言
二、各种断言测试:
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语言标量、结构体或联合体时使用,实际测试发现NSString也可以);

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

推荐大牛博客:TDD的iOS开发初步以及Kiwi使用入门

1 0