天天看點

(轉)【ios】單元測試

一聽到單元測試這個詞感覺很高端,其實單元測試就是為你的方法多專門寫一個測試函數。以保證你的方法在不停的修改開發中。保持正确。如果出錯,第一時間讓你知道,這樣從最小機關開始監控來保證軟體的品質。

什麼時候用到單元測試:

1、寫完代碼以後:想要驗證一下自己寫的代碼是否有問題。

2、寫代碼之前:就是寫代碼之前所有的功能分子產品的設計好,測試通過了再寫。(我反正是沒用過)。

3、修複某個bug後:一般修複完某個bug,為了確定修複是成功的,會寫測試。

怎麼寫單元測試

好像廢話有點多了,還是直接奔主題吧。

建立一個工程,名字随便取,直接勾選include Unit Tests

(轉)【ios】單元測試

萬一我忘了勾選怎麼辦呢?可以有其他方式建立File-->new-->target-->iOS-->iOS Unit Testing Bundle。名字自己看着辦吧。

(轉)【ios】單元測試
(轉)【ios】單元測試

工程建立好後,那要怎麼開始測試呢?

找到系統單元測試Testes檔案夾中.m檔案看中會到看到幾個方法,我們來看下這個幾個方法是什麼時候調用和他們各種的作用

(轉)【ios】單元測試
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都行,

工程就跑起來了

(轉)【ios】單元測試

我們可以在在控制台清晰的看到我們要測試的用例子通過了,測試通過的測試方法會有綠色的鈎。

這時候我們改下斷言,把100随便改成一個數,120.再comand+u運作下,看下什麼情況

(轉)【ios】單元測試

很明顯是能不能通過的,因為我們要測試的方法傳回值是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運作就能看到如圖:

(轉)【ios】單元測試
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沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過