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