Xcode單元測(cè)試

單元測(cè)試
上面的單元測(cè)試的百度詞條解釋,下面咱們就來說一下Xcode上單元測(cè)試的使用。

第一步、新建工程,勾選Include Unit Tests,如下圖
49C52B25-1B43-43A8-BDA4-F18D76794945.png
第二步、點(diǎn)擊下一步,創(chuàng)建工程之后,你會(huì)發(fā)現(xiàn)多出一個(gè)ProjectNameTests的文件夾,如下圖
211E7389-BE3D-48D9-8F54-6417C251CC68.png
如果在創(chuàng)建項(xiàng)目時(shí)沒有勾選這一項(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的說明

/*
 *用于在測(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è)試通過,如下

屏幕快照 2017-08-03 上午10.35.45.png

控制臺(tái)輸出

屏幕快照 2017-08-03 上午10.37.16.png

XCTAssertEqual(result, NO,@"測(cè)試沒通過");NO修改為YES,測(cè)試通過,如下

屏幕快照 2017-08-03 上午10.39.44.png

控制臺(tái)輸出

屏幕快照 2017-08-03 上午10.40.03.png

3、testPerformanceExample性能測(cè)試示例的使用和解釋
如下,寫一個(gè)for循環(huán)并輸出值,然后Command+U進(jìn)行測(cè)試,

屏幕快照 2017-08-03 上午10.45.26.png

圖中的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è)試,反之不通過
特別注意下XCTAssertEqualObjectsXCTAssertEqual。
XCTAssertEqualObjects(a1, a2, format...)的判斷條件是[a1 isEqual:a2]是否返回一個(gè)YES。
XCTAssertEqual(a1, a2, format...)的判斷條件是a1 == a2是否返回一個(gè)YES。

文章內(nèi)容參考自 Jymn_Chen的博客,在此對(duì)他表示感謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 學(xué)習(xí)資料 蘋果官方介紹 蘋果官方文檔 Quick文檔 Xcode單元測(cè)試基本用法 整體測(cè)試command + u,...
    劉大帥閱讀 2,788評(píng)論 0 12
  • 根據(jù)測(cè)試的目的大致可以將單元測(cè)試分為這三類: 性能測(cè)試:測(cè)試代碼執(zhí)行花費(fèi)的時(shí)間 邏輯測(cè)試:測(cè)試代碼執(zhí)行結(jié)果是否符合...
    ThaiLanKing閱讀 760評(píng)論 0 0
  • Xcode 單元測(cè)試 添加單元測(cè)試 新建項(xiàng)目時(shí)添加 File->New->Target... 效果 基本流程 單元...
    面試小集閱讀 882評(píng)論 0 0
  • 一顆綠豆只能發(fā)一根豆芽,綠豆這么貴,豆芽才多少錢 我跟妹子搬了新家,出了校門,這幾個(gè)月來少有時(shí)間吃上熱菜,一是沒有...
    一刻寄秋閱讀 229評(píng)論 0 0
  • 朋友都說我是樂觀派,整天嘻嘻哈哈像個(gè)二傻子似的活著,不知人間苦與悲,不消人間愁與憂。好像我的世界充滿美好且無憂...
    原味七分酸閱讀 481評(píng)論 0 0