使用Xcode自帶單元測試UnitTest




轉自:http://www.jianshu.com/p/009844a0b9ed

什麼時候用到單元測試:

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

怎麼寫單元測試

好像廢話有點多了,還是直接奔主題吧。
創建一個工程,名字隨便取,直接勾選include Unit Tests


QQ20160129-0.png


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




QQ20160129-1.png

工程創建好後,那要怎麼開始測試呢?
找到系統單元測試Testes文件夾中.m文件看中會到看到幾個方法,我們來看下這個幾個方法是什麼時候調用和他們各種的作用


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

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


QQ20160129-3.png


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

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

QQ20160129-4.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運行就能看到如圖:


QQ20160129-5.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沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過


轉自:http://www.jianshu.com/p/009844a0b9ed

什麼時候用到單元測試:

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

怎麼寫單元測試

好像廢話有點多了,還是直接奔主題吧。
創建一個工程,名字隨便取,直接勾選include Unit Tests


QQ20160129-0.png


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




QQ20160129-1.png

工程創建好後,那要怎麼開始測試呢?
找到系統單元測試Testes文件夾中.m文件看中會到看到幾個方法,我們來看下這個幾個方法是什麼時候調用和他們各種的作用


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

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


QQ20160129-3.png


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

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

QQ20160129-4.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運行就能看到如圖:


QQ20160129-5.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沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章