iOS-使用Xcode自帶單元測試UnitTest

![Uploading QQ20160129-3_262826.png . . .]####什么是單元測試?
一聽到單元測試這個(gè)詞感覺很高端,其實(shí)單元測試就是為你的方法多專門寫一個(gè)測試函數(shù)。以保證你的方法在不停的修改開發(fā)中。保持正確。如果出錯(cuò),第一時(shí)間讓你知道,這樣從最小單位開始監(jiān)控來保證軟件的質(zhì)量。

什么時(shí)候用到單元測試:

1、寫完代碼以后:想要驗(yàn)證一下自己寫的代碼是否有問題。
2、寫代碼之前:就是寫代碼之前所有的功能分模塊的設(shè)計(jì)好,測試通過了再寫。(我反正是沒用過)。
3、修復(fù)某個(gè)bug后:一般修復(fù)完某個(gè)bug,為了確保修復(fù)是成功的,會(huì)寫測試。

怎么寫單元測試

好像廢話有點(diǎn)多了,還是直接奔主題吧。
創(chuàng)建一個(gè)工程,名字隨便取,直接勾選include Unit Tests


QQ20160129-0.png

萬一我忘了勾選怎么辦呢?可以有其他方式創(chuàng)建File-->new-->target-->iOS-->iOS Unit Testing Bundle。名字自己看著辦吧。

![QQ20160129-1.png](http://upload-images.jianshu.io/upload_images/970305-4904c381bf62da67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

工程創(chuàng)建好后,那要怎么開始測試呢?
找到系統(tǒng)單元測試Testes文件夾中.m文件看中會(huì)到看到幾個(gè)方法,我們來看下這個(gè)幾個(gè)方法是什么時(shí)候調(diào)用和他們各種的作用

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.
//初始化的代碼,在測試方法調(diào)用之前調(diào)用
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// 釋放測試用例的資源代碼,這個(gè)方法會(huì)每個(gè)測試用例執(zhí)行后調(diào)用
[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中寫一個(gè)簡單的方法

- (int)getNum;

實(shí)現(xiàn):

- (int)getNum {

return 100;
}

在測試的文件中導(dǎo)入ViewController.h,并且定義一個(gè)vc屬性

 #import <XCTest/XCTest.h>

#import "ViewController.h"

@interface ____Tests : XCTestCase

@property (nonatomic,strong) ViewController *vc;


@end

@implementation ____Tests

測試用例的實(shí)現(xiàn)
- (void)setUp {
[super setUp];

// 實(shí)例化需要測試的類
self.vc = [[ViewController alloc] init];
}

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

[super tearDown];
}

- (void)testMyFuc {

// 調(diào)用需要測試的方法,
int result = [self.vc getNum];
// 如果不相等則會(huì)提示@“測試不通過”
XCTAssertEqual(result, 100,@"測試不通過");
}

command+u快捷方式運(yùn)行,或者produce-->test都行,
工程就跑起來了

QQ20160129-3.png

我們可以在在控制臺(tái)清晰的看到我們要測試的用例子通過了,測試通過的測試方法會(huì)有綠色的鉤。

這時(shí)候我們改下斷言,把100隨便改成一個(gè)數(shù),120.再comand+u運(yùn)行下,看下什么情況
QQ20160129-4.png

很明顯是能不能通過的,因?yàn)槲覀円獪y試的方法返回值是100,

自帶的測試框架還能測試某個(gè)方法的性能,

- (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");
    }
}];

}

我們在例子中添加一個(gè)for循環(huán),測試其性能。command+u運(yùn)行就能看到如圖:

QQ20160129-5.png

能夠非常直觀的看出其調(diào)用的時(shí)間,可以用其來對比性能的優(yōu)劣。

另外XCTest還支持異步單元測試,我就不在這里展開了。最后附上常用的斷言及解釋。

  XCTFail(format…) 生成一個(gè)失敗的測試; 
XCTAssertNil(a1, format...)為空判斷,a1為空時(shí)通過,反之不通過;
XCTAssertNotNil(a1, format…)不為空判斷,a1不為空時(shí)通過,反之不通過;
XCTAssert(expression, format...)當(dāng)expression求值為TRUE時(shí)通過;
XCTAssertTrue(expression, format...)當(dāng)expression求值為TRUE時(shí)通過;
XCTAssertFalse(expression, format...)當(dāng)expression求值為False時(shí)通過;
XCTAssertEqualObjects(a1, a2, format...)判斷相等,[a1 isEqual:a2]值為TRUE時(shí)通過,其中一個(gè)不為空時(shí),不通過;
XCTAssertNotEqualObjects(a1, a2, format...)判斷不等,[a1 isEqual:a2]值為False時(shí)通過;
XCTAssertEqual(a1, a2, format...)判斷相等(當(dāng)a1和a2是 C語言標(biāo)量、結(jié)構(gòu)體或聯(lián)合體時(shí)使用, 判斷的是變量的地址,如果地址相同則返回TRUE,否則返回NO);
XCTAssertNotEqual(a1, a2, format...)判斷不等(當(dāng)a1和a2是 C語言標(biāo)量、結(jié)構(gòu)體或聯(lián)合體時(shí)使用);
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判斷相等,(double或float類型)提供一個(gè)誤差范圍,當(dāng)在誤差范圍(+/-accuracy)以內(nèi)相等時(shí)通過測試;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判斷不等,(double或float類型)提供一個(gè)誤差范圍,當(dāng)在誤差范圍以內(nèi)不等時(shí)通過測試;
XCTAssertThrows(expression, format...)異常測試,當(dāng)expression發(fā)生異常時(shí)通過;反之不通過;(很變態(tài)) XCTAssertThrowsSpecific(expression, specificException, format...) 異常測試,當(dāng)expression發(fā)生specificException異常時(shí)通過;反之發(fā)生其他異常或不發(fā)生異常均不通過;
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)異常測試,當(dāng)expression發(fā)生具體異常、具體異常名稱的異常時(shí)通過測試,反之不通過;
XCTAssertNoThrow(expression, format…)異常測試,當(dāng)expression沒有發(fā)生異常時(shí)通過測試;
XCTAssertNoThrowSpecific(expression, specificException, format...)異常測試,當(dāng)expression沒有發(fā)生具體異常、具體異常名稱的異常時(shí)通過測試,反之不通過;
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)異常測試,當(dāng)expression沒有發(fā)生具體異常、具體異常名稱的異常時(shí)通過測試,反之不通過
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容