單元測(cè)試
上面的單元測(cè)試的百度詞條解釋,下面咱們就來說一下Xcode
上單元測(cè)試的使用。
第一步、新建工程,勾選Include Unit Tests
,如下圖
第二步、點(diǎn)擊下一步,創(chuàng)建工程之后,你會(huì)發(fā)現(xiàn)多出一個(gè)ProjectNameTests
的文件夾,如下圖
如果在創(chuàng)建項(xiàng)目時(shí)沒有勾選這一項(xiàng),也可以通過下面的方式來創(chuàng)建,如下圖
第三步、UnitTestDemoTests.m
的說明和使用
1、UnitTestDemoTests.m
的說明
/*
*用于在測(cè)試前設(shè)置好要測(cè)試的方法,在測(cè)試方法調(diào)用之前調(diào)用,如,初始化的代碼
*/
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
/*
*用于在測(cè)試后將設(shè)置好的要測(cè)試的方法拆卸掉,釋放資源
*/
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
/*
*測(cè)試示例,一定要以test開頭
*比如,你可以創(chuàng)建, - (void)testMyProject{}
*/
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
/*
*性能測(cè)試示例
*/
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
//在這里存放需要測(cè)試性能的代碼
}];
}
2、使用
在ViewController
中聲明一個(gè)函數(shù)并實(shí)現(xiàn),如,
- (BOOL)getMyBoolValue;
...
...
- (BOOL)getMyBoolValue
{
return YES;
}
在UnitTestDemoTests.m
中導(dǎo)入ViewController
的頭文件,聲明一個(gè)ViewController
的對(duì)象并在setUp
方法中初始化。如下,
#import <XCTest/XCTest.h>
#import "ViewController.h"
@interface UnitTestDemoTests : XCTestCase
@property (nonatomic,strong) ViewController *viewController;
@end
@implementation UnitTestDemoTests
/*
*用于在測(cè)試前設(shè)置好要測(cè)試的方法,在測(cè)試方法調(diào)用之前調(diào)用,如,初始化的代碼
*/
- (void)setUp {
[super setUp];
self.viewController = [[ViewController alloc]init];//初始化
}
/*
*用于在測(cè)試后將設(shè)置好的要測(cè)試的方法拆卸掉,釋放資源
*/
- (void)tearDown {
self.viewController = nil;//釋放
[super tearDown];
}
/*
*測(cè)試示例,一定要以test開頭
*比如,你可以創(chuàng)建, - (void)testMyProject{}
*/
- (void)testMyBoolFunc
{
BOOL result = [self.viewController getMyBoolValue];
XCTAssertEqual(result, NO,@"測(cè)試沒通過");
}
接著,Command+U
進(jìn)行測(cè)試,然而,控制臺(tái)可能會(huì)輸出類似下面的錯(cuò)誤提示:
Connection peer refused channel request forr"dtxproxy:XCTestDriverInterface:XCTestManager_IDEInterface"; channel canceled Failed to run tests: The operation couldn’t be completed. (DTXProxyChannel error 1.)
這時(shí)候,就要先Command + R
運(yùn)行一下項(xiàng)目,然后再Command+U
進(jìn)行測(cè)試。
當(dāng)然,上面的例子沒有測(cè)試通過,如下
控制臺(tái)輸出
把XCTAssertEqual(result, NO,@"測(cè)試沒通過");
中NO
修改為YES
,測(cè)試通過,如下
控制臺(tái)輸出
3、testPerformanceExample
性能測(cè)試示例的使用和解釋
如下,寫一個(gè)for循環(huán)
并輸出值,然后Command+U
進(jìn)行測(cè)試,
圖中的
0.145 sec
只是CPU運(yùn)算的時(shí)間,時(shí)間顯示和輸出的時(shí)間可能比這個(gè)值要大一些4、最后介紹一下
XCTAssertEqual
以及相關(guān)的宏定義函數(shù)。
XCTFail(format…) 生成一個(gè)失敗的測(cè)試;
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語(yǔ)言標(biāo)量、結(jié)構(gòu)體或聯(lián)合體時(shí)使用,實(shí)際測(cè)試發(fā)現(xiàn)NSString也可以);
XCTAssertNotEqual(a1, a2, format...)判斷不等(當(dāng)a1和a2是 C語(yǔ)言標(biāo)量、結(jié)構(gòu)體或聯(lián)合體時(shí)使用);
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判斷相等,(double或float類型)提供一個(gè)誤差范圍,當(dāng)在誤差范圍(+/-accuracy)以內(nèi)相等時(shí)通過測(cè)試;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判斷不等,(double或float類型)提供一個(gè)誤差范圍,當(dāng)在誤差范圍以內(nèi)不等時(shí)通過測(cè)試;
XCTAssertThrows(expression, format...)異常測(cè)試,當(dāng)expression發(fā)生異常時(shí)通過;反之不通過;(很變態(tài))
XCTAssertThrowsSpecific(expression, specificException, format...) 異常測(cè)試,當(dāng)expression發(fā)生specificException異常時(shí)通過;反之發(fā)生其他異?;虿话l(fā)生異常均不通過;
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)異常測(cè)試,當(dāng)expression發(fā)生具體異常、具體異常名稱的異常時(shí)通過測(cè)試,反之不通過;
XCTAssertNoThrow(expression, format…)異常測(cè)試,當(dāng)expression沒有發(fā)生異常時(shí)通過測(cè)試;
XCTAssertNoThrowSpecific(expression, specificException, format...)異常測(cè)試,當(dāng)expression沒有發(fā)生具體異常、具體異常名稱的異常時(shí)通過測(cè)試,反之不通過;
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)異常測(cè)試,當(dāng)expression沒有發(fā)生具體異常、具體異常名稱的異常時(shí)通過測(cè)試,反之不通過
特別注意下XCTAssertEqualObjects
和XCTAssertEqual
。
XCTAssertEqualObjects(a1, a2, format...)的判斷條件是[a1 isEqual:a2]是否返回一個(gè)YES。
XCTAssertEqual(a1, a2, format...)的判斷條件是a1 == a2是否返回一個(gè)YES。
文章內(nèi)容參考自 Jymn_Chen的博客,在此對(duì)他表示感謝。