成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺析iOS單元測(cè)試

開(kāi)發(fā) 開(kāi)發(fā)工具
單元測(cè)試作為敏捷開(kāi)發(fā)實(shí)踐的組成之一,其目的是提高軟件開(kāi)發(fā)的效率,維持代碼的健康性。其目標(biāo)是證明軟件能夠正常運(yùn)行,而不是發(fā)現(xiàn)bug(發(fā)現(xiàn)bug這一目的與開(kāi)發(fā)成本是正相關(guān)的,雖然發(fā)現(xiàn)bug是保證軟件質(zhì)量的一種手段,但是很顯然這與降低軟件開(kāi)發(fā)成本這一目的背道而馳)。

 單元測(cè)試作為敏捷開(kāi)發(fā)實(shí)踐的組成之一,其目的是提高軟件開(kāi)發(fā)的效率,維持代碼的健康性。其目標(biāo)是證明軟件能夠正常運(yùn)行,而不是發(fā)現(xiàn)bug(發(fā)現(xiàn)bug這一目的與開(kāi)發(fā)成本是正相關(guān)的,雖然發(fā)現(xiàn)bug是保證軟件質(zhì)量的一種手段,但是很顯然這與降低軟件開(kāi)發(fā)成本這一目的背道而馳)。

[[325092]]

單元測(cè)試是對(duì)軟件質(zhì)量的一種保證,例如重構(gòu)之后我們需要保證軟件產(chǎn)品的正常運(yùn)行。而iOS非常幸運(yùn),蘋(píng)果開(kāi)發(fā)工具Xcode在創(chuàng)建項(xiàng)目是就能夠自帶XCTest,包含單元測(cè)試和UI測(cè)試,這次我們從兩個(gè)方面講一下單元測(cè)試。

一、 開(kāi)發(fā)

如果創(chuàng)建工程時(shí)自帶,則可以在工程項(xiàng)目文件中的TARGETS看到有一個(gè)對(duì)應(yīng)工程名,以Tests結(jié)尾的項(xiàng)目,如果沒(méi)有,也可以自己創(chuàng)建,點(diǎn)擊下方的加號(hào),輸入test即可看到對(duì)應(yīng)的bundle,這里我們選擇Unit Testing Bundle。

創(chuàng)建后會(huì)多一個(gè)文件夾,在其中創(chuàng)建和剛創(chuàng)建的項(xiàng)目名稱(chēng)相同,在里面就可以創(chuàng)建每個(gè)文件的單元測(cè)試了。

對(duì)文件夾右鍵點(diǎn)擊NewFile,選擇Unit Test Case Class即可創(chuàng)建一個(gè)單元測(cè)試文件。

創(chuàng)建過(guò)程就到這里了。接下來(lái)針對(duì)單元測(cè)試一些使用進(jìn)行簡(jiǎn)單描述。

首先單元測(cè)試是為了測(cè)試方法的可行性,所以需要斷言來(lái)看是否正確,XCTest提供了許多斷言可用,這里就列舉一些常見(jiàn)的斷言:

XCTAssertNotNil(expression, ...)

XCTAssertNil(expression, ...)

XCTAssertTrue(expression, ...)

XCTAssertFalse(expression, ...)

XCTAssertEqualObjects(expression1, expression2, ...)

XCTAssertEqual(expression1, expression2, ...)

XCTAssertGreaterThan(expression1, expression2, ...)

還有更多可以通過(guò)XCTestAssertions.h來(lái)查找。

這里的斷言很容易理解,可以通過(guò)方法名釋義,比如第一個(gè)XCTAssertNotNil就是必須表達(dá)式或?qū)ο蟛粸榭詹拍芡ㄟ^(guò),否則測(cè)試失敗。

接下來(lái)我們用一個(gè)簡(jiǎn)單的例子來(lái)看一下:

這兒有個(gè)簡(jiǎn)單的實(shí)現(xiàn)加法的類(lèi)以及方法。

  1. #import <Foundation/Foundation.h> 
  2.  
  3. NS_ASSUME_NONNULL_BEGIN 
  4.  
  5. @interface CalcMethod : NSObject 
  6.  
  7. + (NSInteger)plus:(NSInteger)a andB:(NSInteger)b; 
  8.  
  9. @end 
  10.  
  11. NS_ASSUME 

 

  1. #import "CalcMethod.h" 
  2.  
  3. @implementation CalcMethod 
  4.  
  5. + (NSInteger)plus:(NSInteger)a andB:(NSInteger)b { 
  6.     return a + b; 
  7.  
  8. @end 

實(shí)現(xiàn)它的單元測(cè)試

首先先新建單元測(cè)試文件,這個(gè)只有.m,沒(méi)有頭文件。

  1. #import <XCTest/XCTest.h> 
  2.  
  3. @interface CalcMethodTests : XCTestCase 
  4.  
  5. @end 
  6.  
  7. @implementation CalcMethodTests 
  8.  
  9. - (void)setUp { 
  10.     // Put setup code here. This method is called before the invocation of each test method in the class. 
  11.  
  12. - (void)tearDown { 
  13.     // Put teardown code here. This method is called after the invocation of each test method in the class. 
  14.  
  15. - (void)testExample { 
  16.     // This is an example of a functional test case
  17.     // Use XCTAssert and related functions to verify your tests produce the correct results. 
  18.  
  19. - (void)testPerformanceExample { 
  20.     // This is an example of a performance test case
  21.     [self measureBlock:^{ 
  22.         // Put the code you want to measure the time of here. 
  23.     }]; 

可以看到系統(tǒng)給出了幾個(gè)方法,其中setUp是每個(gè)方法執(zhí)行測(cè)試前會(huì)調(diào)用用來(lái)初始化一些參數(shù);tearDown是每個(gè)方法執(zhí)行完成之后實(shí)行的一些銷(xiāo)毀方法;testExample是用來(lái)具體測(cè)試的方法,也可以自己定義,但必須以test開(kāi)頭;testPerformanceExample是用來(lái)測(cè)試性能的,放在measureBlock中,會(huì)運(yùn)行10次測(cè)每次所使用的時(shí)間,一般用來(lái)或者大量計(jì)算所產(chǎn)生的耗時(shí)。

這里只是單純的加法運(yùn)算,就不需要了。添加測(cè)試方法:

  1. - (void)testCalcMethod { 
  2.     XCTAssertEqual([CalcMethod plus:1 andB:2], 3, @"1+2=3"); 

然后切換Scheme到對(duì)應(yīng)的Test中,如果沒(méi)有則可以在管理Scheme中添加,然后點(diǎn)擊方法前的菱形塊即可測(cè)試該方法,或者點(diǎn)command+u將所有測(cè)試文件和方法進(jìn)行單元測(cè)試。

左側(cè)切到測(cè)試欄,這樣可以更便捷的點(diǎn)擊測(cè)試,測(cè)試通過(guò)就會(huì)顯示勾。

二、 常見(jiàn)問(wèn)題處理

1. 在第一次執(zhí)行單元測(cè)試時(shí)發(fā)現(xiàn)編譯不過(guò),報(bào)找不到類(lèi)型

原因是單元測(cè)試對(duì)引用要求更嚴(yán)格,以前在編譯中可能會(huì)直接通過(guò),但單元測(cè)試不行,解決辦法也很簡(jiǎn)單,對(duì)所有相關(guān)文件引入對(duì)應(yīng)頭文件即可。

這個(gè)只是針對(duì)上方的報(bào)錯(cuò),也可能有更多的錯(cuò)誤,需要自己進(jìn)行配對(duì)。

如果有在A(yíng)pp中有接入自己開(kāi)發(fā)的Framework工程,并且要對(duì)Framework進(jìn)行單元測(cè)試,接下來(lái)是針對(duì)Framework中的一些單元測(cè)試問(wèn)題

2. 找不到第三方庫(kù)或者pod的頭文件

這是因?yàn)槿绻愕膄ramework通過(guò)pod引入,那么pod頭文件管理不需要你自己來(lái)處理,pod會(huì)處理完并集成到App中。但單元測(cè)試不行,所以需要對(duì)你自己的Framework以及單元測(cè)試的bundle添加headers。

切到工程文件的Build SettingsàHeader Search Paths,加入你自己對(duì)應(yīng)Pod的頭文件路徑,包括單元測(cè)試的bundle與單元測(cè)試對(duì)應(yīng)的Framework工程都需要添加。

3. 報(bào)Include of non-modular header inside framework module

這個(gè)仍舊在單元測(cè)試bundle的Build SettingsàAllow Non-modular Includes In Framework Modules,將此設(shè)置改為YES即可。

4. 使用pod集成后,App調(diào)試報(bào)找不到XCTest

這是因?yàn)閜od包含文件太粗糙,使用**代替所有子目錄,導(dǎo)致單元測(cè)試的.m都一起被包含到pod文件中。

解決辦法一:精確pod需要的文件路徑

解決辦法二:規(guī)范單元測(cè)試文件命名,并在pod配置中排除

5. 如果要測(cè)一些網(wǎng)絡(luò)請(qǐng)求或異步操作怎么辦?

如果直接在test方法中寫(xiě)入一些異步方法,在回調(diào)中在進(jìn)行斷言,會(huì)發(fā)現(xiàn)無(wú)論正確與否都會(huì)直接通過(guò)。所以蘋(píng)果也提供了一個(gè)為單元測(cè)試準(zhǔn)備的異步阻斷。

  1. /*! 
  2.  * @method -waitForExpectationsWithTimeout:handler: 
  3.  * 
  4.  * @param timeout 
  5.  * The amount of time within which all expectations must be fulfilled. 
  6.  * 
  7.  * @param handler 
  8.  * If provided, the handler will be invoked both on timeout or fulfillment of all 
  9.  * expectations. Timeout is always treated as a test failure. 
  10.  * 
  11.  * @discussion 
  12.  * -waitForExpectationsWithTimeout:handler: creates a point of synchronization in the flow of a 
  13.  * test. Only one -waitForExpectationsWithTimeout:handler: can be active at any given time, but 
  14.  * multiple discrete sequences of { expectations -> wait } can be chained together. 
  15.  * 
  16.  * -waitForExpectationsWithTimeout:handler: runs the run loop while handling events until all expectations 
  17.  * are fulfilled or the timeout is reached. Clients should not manipulate the run 
  18.  * loop while using this API. 
  19.  */ 
  20. - (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout handler:(nullable XCWaitCompletionHandler)handler; 
  21.  
  22. /*! 
  23.  * @method -expectationForNotification:object:handler: 
  24.  * 
  25.  * @discussion 
  26.  * A convenience method for asynchronous tests that observe NSNotifications from the default 
  27.  * NSNotificationCenter. 
  28.  * 
  29.  * @param notificationName 
  30.  * The notification to register for
  31.  * 
  32.  * @param objectToObserve 
  33.  * The object to observe. 
  34.  * 
  35.  * @param handler 
  36.  * Optional handler, /see XCNotificationExpectationHandler. If not provided, the expectation 
  37.  * will be fulfilled by the first notification matching the specified name from the 
  38.  * observed object. 
  39.  * 
  40.  * @return 
  41.  * Creates and returns an expectation associated with the test case
  42.  */ 
  43. - (XCTestExpectation *)expectationForNotification:(NSNotificationName)notificationName  

方法我單獨(dú)提出來(lái)常用的兩個(gè),這樣可以阻塞線(xiàn)程,并在收到通知或者超時(shí)后再繼續(xù),更多方法詳見(jiàn)XCTestCase+AsynchronousTesting.h。

下面是使用方法:

  1. (void)testAsync { 
  2.      
  3.     dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
  4.         // Do Some things async 
  5.         // XCAssert 
  6.          
  7.         [[NSNotificationCenter defaultCenter] postNotificationName:@"UnitTestsNotify" object:nil]; 
  8.     }); 
  9.      
  10.     do { 
  11.         [self expectationForNotification:@"UnitTestsNotify" object:nil handler:nil]; 
  12.         [self waitForExpectationsWithTimeout:30 handler:nil]; 
  13.     } while (0); 
  14.  

也可以使用宏定義將這方法簡(jiǎn)化,更直觀(guān),也更方便多次調(diào)用。

  1. #define WAIT do {\  
  2. [self expectationForNotification:@"UnitTestsNotify" object:nil handler:nil];\  
  3. [self waitForExpectationsWithTimeout:30 handler:nil];\  
  4. } while (0);  
  5.   
  6. #define NOTIFY \  
  7. [[NSNotificationCenter defaultCenter] postNotificationName:@"UnitTestsNotify" object:nil];  
  8.   
  9. - (void)testAsync {  
  10.       
  11.     dispatch_async(dispatch_get_global_queue(0, 0), ^{  
  12.         // Do Some things async  
  13.         // XCAssert  
  14.           
  15.         NOTIFY  
  16.     });  
  17.       
  18.     WAIT  
  19. }  

【本文是51CTO專(zhuān)欄機(jī)構(gòu)“AiChinaTech”的原創(chuàng)文章,微信公眾號(hào)( id: tech-AI)”】

戳這里,看該作者更多好文 

 

責(zé)任編輯:華軒 來(lái)源: 51CTO專(zhuān)欄
相關(guān)推薦

2009-07-23 16:29:06

ASP.NET單元測(cè)試

2009-06-26 17:48:38

JSF項(xiàng)目單元測(cè)試JSFUnit

2009-09-01 16:35:16

C#單元測(cè)試

2017-01-14 23:42:49

單元測(cè)試框架軟件測(cè)試

2012-05-21 09:41:54

XcodeiOS單元測(cè)試

2009-09-01 16:10:28

C#單元測(cè)試

2012-03-30 15:52:51

ibmdw

2017-03-28 12:25:36

2009-07-21 16:30:15

iBATIS.NET與單元測(cè)試

2017-01-14 23:26:17

單元測(cè)試JUnit測(cè)試

2017-01-16 12:12:29

單元測(cè)試JUnit

2020-08-18 08:10:02

單元測(cè)試Java

2021-05-05 11:38:40

TestNGPowerMock單元測(cè)試

2017-03-23 16:02:10

Mock技術(shù)單元測(cè)試

2011-07-04 18:16:42

單元測(cè)試

2015-05-08 10:29:59

OCMockiOS測(cè)試

2022-04-27 08:17:07

OCMock單元測(cè)試集成

2023-07-26 08:58:45

Golang單元測(cè)試

2009-09-01 16:20:27

C#單元測(cè)試

2009-09-01 16:29:01

C#單元測(cè)試
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 亚洲va在线va天堂va狼色在线 | 亚洲成人黄色 | 亚洲视频中文字幕 | 一级a性色生活片久久毛片波多野 | 在线黄色网 | 日韩av电影在线观看 | 国产在线中文字幕 | 日韩一区中文字幕 | 999国产视频| 精品一区二区三区四区五区 | 国产一级特黄真人毛片 | 成年人在线观看视频 | 精品国产久| 国产一区二区欧美 | 99精品免费久久久久久久久日本 | 一级免费看 | 日本精品视频在线 | 日韩欧美在线免费观看视频 | 欧美久久久网站 | а天堂中文最新一区二区三区 | 一区二区av| 中文字幕国产视频 | 波霸ol一区二区 | 精品人伦一区二区三区蜜桃网站 | 99精品免费久久久久久日本 | 国产第二页| 国产在线观看不卡一区二区三区 | 国产福利视频网站 | av天天干 | 人碰人操 | k8久久久一区二区三区 | 久久99精品久久久 | av在线播放不卡 | 亚洲免费在线视频 | 欧美日韩精品一区二区三区蜜桃 | 成人精品视频在线观看 | 欧美伊人 | 国产色婷婷精品综合在线手机播放 | 在线看一区二区三区 | 中文字幕在线观看一区 | 网站黄色av |