自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析iOS單元測試

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

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

[[325092]]

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

一、 開發(fā)

如果創(chuàng)建工程時自帶,則可以在工程項目文件中的TARGETS看到有一個對應工程名,以Tests結尾的項目,如果沒有,也可以自己創(chuàng)建,點擊下方的加號,輸入test即可看到對應的bundle,這里我們選擇Unit Testing Bundle。

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

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

創(chuàng)建過程就到這里了。接下來針對單元測試一些使用進行簡單描述。

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

XCTAssertNotNil(expression, ...)

XCTAssertNil(expression, ...)

XCTAssertTrue(expression, ...)

XCTAssertFalse(expression, ...)

XCTAssertEqualObjects(expression1, expression2, ...)

XCTAssertEqual(expression1, expression2, ...)

XCTAssertGreaterThan(expression1, expression2, ...)

還有更多可以通過XCTestAssertions.h來查找。

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

接下來我們用一個簡單的例子來看一下:

這兒有個簡單的實現(xiàn)加法的類以及方法。

  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 

實現(xiàn)它的單元測試

首先先新建單元測試文件,這個只有.m,沒有頭文件。

  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)給出了幾個方法,其中setUp是每個方法執(zhí)行測試前會調(diào)用用來初始化一些參數(shù);tearDown是每個方法執(zhí)行完成之后實行的一些銷毀方法;testExample是用來具體測試的方法,也可以自己定義,但必須以test開頭;testPerformanceExample是用來測試性能的,放在measureBlock中,會運行10次測每次所使用的時間,一般用來或者大量計算所產(chǎn)生的耗時。

這里只是單純的加法運算,就不需要了。添加測試方法:

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

然后切換Scheme到對應的Test中,如果沒有則可以在管理Scheme中添加,然后點擊方法前的菱形塊即可測試該方法,或者點command+u將所有測試文件和方法進行單元測試。

左側切到測試欄,這樣可以更便捷的點擊測試,測試通過就會顯示勾。

二、 常見問題處理

1. 在第一次執(zhí)行單元測試時發(fā)現(xiàn)編譯不過,報找不到類型

原因是單元測試對引用要求更嚴格,以前在編譯中可能會直接通過,但單元測試不行,解決辦法也很簡單,對所有相關文件引入對應頭文件即可。

這個只是針對上方的報錯,也可能有更多的錯誤,需要自己進行配對。

如果有在App中有接入自己開發(fā)的Framework工程,并且要對Framework進行單元測試,接下來是針對Framework中的一些單元測試問題

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

這是因為如果你的framework通過pod引入,那么pod頭文件管理不需要你自己來處理,pod會處理完并集成到App中。但單元測試不行,所以需要對你自己的Framework以及單元測試的bundle添加headers。

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

3. 報Include of non-modular header inside framework module

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

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

這是因為pod包含文件太粗糙,使用**代替所有子目錄,導致單元測試的.m都一起被包含到pod文件中。

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

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

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

如果直接在test方法中寫入一些異步方法,在回調(diào)中在進行斷言,會發(fā)現(xià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  

方法我單獨提出來常用的兩個,這樣可以阻塞線程,并在收到通知或者超時后再繼續(xù),更多方法詳見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.  

也可以使用宏定義將這方法簡化,更直觀,也更方便多次調(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專欄機構“AiChinaTech”的原創(chuàng)文章,微信公眾號( id: tech-AI)”】

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

 

責任編輯:華軒 來源: 51CTO專欄
相關推薦

2009-07-23 16:29:06

ASP.NET單元測試

2009-09-01 16:35:16

C#單元測試

2009-06-26 17:48:38

JSF項目單元測試JSFUnit

2017-01-14 23:42:49

單元測試框架軟件測試

2012-05-21 09:41:54

XcodeiOS單元測試

2017-03-28 12:25:36

2012-03-30 15:52:51

ibmdw

2009-09-01 16:10:28

C#單元測試

2009-07-21 16:30:15

iBATIS.NET與單元測試

2017-01-14 23:26:17

單元測試JUnit測試

2017-01-16 12:12:29

單元測試JUnit

2022-04-27 08:17:07

OCMock單元測試集成

2015-05-08 10:29:59

OCMockiOS測試

2020-08-18 08:10:02

單元測試Java

2023-07-26 08:58:45

Golang單元測試

2017-03-23 16:02:10

Mock技術單元測試

2011-07-04 18:16:42

單元測試

2021-05-05 11:38:40

TestNGPowerMock單元測試

2009-09-01 16:29:01

C#單元測試

2009-09-01 16:20:27

C#單元測試
點贊
收藏

51CTO技術棧公眾號