淺析iOS單元測試
單元測試作為敏捷開發(fā)實踐的組成之一,其目的是提高軟件開發(fā)的效率,維持代碼的健康性。其目標是證明軟件能夠正常運行,而不是發(fā)現(xiàn)bug(發(fā)現(xiàn)bug這一目的與開發(fā)成本是正相關的,雖然發(fā)現(xiàn)bug是保證軟件質(zhì)量的一種手段,但是很顯然這與降低軟件開發(fā)成本這一目的背道而馳)。
單元測試是對軟件質(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)加法的類以及方法。
- #import <Foundation/Foundation.h>
- NS_ASSUME_NONNULL_BEGIN
- @interface CalcMethod : NSObject
- + (NSInteger)plus:(NSInteger)a andB:(NSInteger)b;
- @end
- NS_ASSUME
- #import "CalcMethod.h"
- @implementation CalcMethod
- + (NSInteger)plus:(NSInteger)a andB:(NSInteger)b {
- return a + b;
- }
- @end
實現(xiàn)它的單元測試
首先先新建單元測試文件,這個只有.m,沒有頭文件。
- #import <XCTest/XCTest.h>
- @interface CalcMethodTests : XCTestCase
- @end
- @implementation CalcMethodTests
- - (void)setUp {
- // Put setup code here. This method is called before the invocation of each test method in the class.
- }
- - (void)tearDown {
- // Put teardown code here. This method is called after the invocation of each test method in the class.
- }
- - (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.
- }];
可以看到系統(tǒng)給出了幾個方法,其中setUp是每個方法執(zhí)行測試前會調(diào)用用來初始化一些參數(shù);tearDown是每個方法執(zhí)行完成之后實行的一些銷毀方法;testExample是用來具體測試的方法,也可以自己定義,但必須以test開頭;testPerformanceExample是用來測試性能的,放在measureBlock中,會運行10次測每次所使用的時間,一般用來或者大量計算所產(chǎn)生的耗時。
這里只是單純的加法運算,就不需要了。添加測試方法:
- - (void)testCalcMethod {
- 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)無論正確與否都會直接通過。所以蘋果也提供了一個為單元測試準備的異步阻斷。
- /*!
- * @method -waitForExpectationsWithTimeout:handler:
- *
- * @param timeout
- * The amount of time within which all expectations must be fulfilled.
- *
- * @param handler
- * If provided, the handler will be invoked both on timeout or fulfillment of all
- * expectations. Timeout is always treated as a test failure.
- *
- * @discussion
- * -waitForExpectationsWithTimeout:handler: creates a point of synchronization in the flow of a
- * test. Only one -waitForExpectationsWithTimeout:handler: can be active at any given time, but
- * multiple discrete sequences of { expectations -> wait } can be chained together.
- *
- * -waitForExpectationsWithTimeout:handler: runs the run loop while handling events until all expectations
- * are fulfilled or the timeout is reached. Clients should not manipulate the run
- * loop while using this API.
- */
- - (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout handler:(nullable XCWaitCompletionHandler)handler;
- /*!
- * @method -expectationForNotification:object:handler:
- *
- * @discussion
- * A convenience method for asynchronous tests that observe NSNotifications from the default
- * NSNotificationCenter.
- *
- * @param notificationName
- * The notification to register for.
- *
- * @param objectToObserve
- * The object to observe.
- *
- * @param handler
- * Optional handler, /see XCNotificationExpectationHandler. If not provided, the expectation
- * will be fulfilled by the first notification matching the specified name from the
- * observed object.
- *
- * @return
- * Creates and returns an expectation associated with the test case.
- */
- - (XCTestExpectation *)expectationForNotification:(NSNotificationName)notificationName
方法我單獨提出來常用的兩個,這樣可以阻塞線程,并在收到通知或者超時后再繼續(xù),更多方法詳見XCTestCase+AsynchronousTesting.h。
下面是使用方法:
- (void)testAsync {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- // Do Some things async
- // XCAssert
- [[NSNotificationCenter defaultCenter] postNotificationName:@"UnitTestsNotify" object:nil];
- });
- do {
- [self expectationForNotification:@"UnitTestsNotify" object:nil handler:nil];
- [self waitForExpectationsWithTimeout:30 handler:nil];
- } while (0);
- }
也可以使用宏定義將這方法簡化,更直觀,也更方便多次調(diào)用。
- #define WAIT do {\
- [self expectationForNotification:@"UnitTestsNotify" object:nil handler:nil];\
- [self waitForExpectationsWithTimeout:30 handler:nil];\
- } while (0);
- #define NOTIFY \
- [[NSNotificationCenter defaultCenter] postNotificationName:@"UnitTestsNotify" object:nil];
- - (void)testAsync {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- // Do Some things async
- // XCAssert
- NOTIFY
- });
- WAIT
- }
【本文是51CTO專欄機構“AiChinaTech”的原創(chuàng)文章,微信公眾號( id: tech-AI)”】