Xcode 自带单元测试
使用背景
1.為了更快、更高效的開發, 開發過程中經常要寫一些測試用例。 2.在開發過程中,有些很深的界面,需要滿足很多條件、造很多假數據才可以進入的,但是前面那些界面我們根本就沒有涉及修改,只是想進入最后這個界面;比如:訂單支付完成界面。 這時候我們可以寫測試用例,直接讓其進入到想要進入的界面。
測試用例分為:單元測試和UI測試。 Xcode自帶了測試用例文件。一般大的項目以及優秀的開源庫都有測試用例的,比如:AFNetworking。 不會寫的可以到這下載AFNetworking源碼參考。
創建測試用例
1.新建工程文件,勾選測試用例
如果忘記勾選了, 也可以通過新建來創建。 和新建文件一樣。2.新建完的工程 測試用例和其他工程文件,分別屬于不同的target。
3.測試用例文件 新的的測試用例文件,都是這個結構和基本的四個方法。
#import <XCTest/XCTest.h>@interface XcodeTestsTests : XCTestCase@end@implementation XcodeTestsTests/*!* @brief 初始化方法。* 初始化、代碼復用、準備測試條件*/ - (void)setUp {[super setUp];// Put setup code here. This method is called before the invocation of each test method in the class. } /*!* @brief 銷毀方法* 每次測試用例跑完,都會跑這個方法,釋放對象、回收資源、避免干擾*/ - (void)tearDown {// Put teardown code here. This method is called after the invocation of each test method in the class.[super tearDown]; } /*!* @brief 測試用例方法* 注意: 這里方法必須以test開頭,不然識別不了。* 開始測試用例方法,cmd+u 或者直接點擊右側這個小框箭頭即可*/ - (void)testExample {// This is an example of a functional test case.// Use XCTAssert and related functions to verify your tests produce the correct results. }/*!* @brief 性能測試方法* 可以測試某一段方法的性能、耗時情況*/ - (void)testPerformanceExample {// This is an example of a performance test case.[self measureBlock:^{// Put the code you want to measure the time of here.}]; }@end 復制代碼寫測試用例基本都在testExample方法里完成。
注意:問題點
寫了一個最簡單的測試用例,運行不起來, 運行報錯:-[UIApplication applicationState] must be used from main thread only??刂婆_報錯信息:Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]
解決辦法: 打開Xcode的Edit Scheme, 找到Test選項,去掉Runtime Api 檢測選項即可。
以上正常情況下就可以運行了。 下面我們開始寫一個簡答的測試用例。
寫測試用例
測試用例的過程一般分三步,這里以測試ViewController的一個加方法為例:
然后實現方法:
- (NSInteger)addFunction:(NSInteger)a andB:(NSInteger)b {return a + b; }復制代碼2.在測試用例里導入頭文件#import "ViewController.h" 以及初始化對象,準備測試條件。
3.開始測試方法
- (void)testAddFunction {// This is an example of a functional test case.// Use XCTAssert and related functions to verify your tests produce the correct results.//1.創建測試條件NSInteger a = 1, b = 2;//2.進行測試,直接調用測試方法NSInteger sam = [self.VC addFunction:a andB:b];//3.斷言。斷言方法有N多種,詳細見下面。 測試最核心。XCTAssertEqual(sam, 3); //前面這個是測試條件,后面的值是我們期望的值。 如果是正確的就測試通過,否則直接掛掉。} 復制代碼斷言一覽表:
XCTFail(format…) 生成一個失敗的測試; XCTAssertNil(a1, format...)為空判斷,a1為空時通過,反之不通過; XCTAssertNotNil(a1, format…)不為空判斷,a1不為空時通過,反之不通過; XCTAssert(expression, format...)當expression求值為TRUE時通過; XCTAssertTrue(expression, format...)當expression求值為TRUE時通過; XCTAssertFalse(expression, format...)當expression求值為False時通過; XCTAssertEqualObjects(a1, a2, format...)判斷相等,[a1 isEqual:a2]值為TRUE時通過,其中一個不為空時,不通過; XCTAssertNotEqualObjects(a1, a2, format...)判斷不等,[a1 isEqual:a2]值為False時通過; XCTAssertEqual(a1, a2, format...)判斷相等(當a1和a2是 C語言標量、結構體或聯合體時使用, 判斷的是變量的地址,如果地址相同則返回TRUE,否則返回NO); XCTAssertNotEqual(a1, a2, format...)判斷不等(當a1和a2是 C語言標量、結構體或聯合體時使用); XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判斷相等,(double或float類型)提供一個誤差范圍,當在誤差范圍(+/-accuracy)以內相等時通過測試; XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判斷不等,(double或float類型)提供一個誤差范圍,當在誤差范圍以內不等時通過測試; XCTAssertThrows(expression, format...)異常測試,當expression發生異常時通過;反之不通過;(很變態) XCTAssertThrowsSpecific(expression, specificException, format...) 異常測試,當expression發生specificException異常時通過;反之發生其他異常或不發生異常均不通過; XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)異常測試,當expression發生具體異常、具體異常名稱的異常時通過測試,反之不通過; XCTAssertNoThrow(expression, format…)異常測試,當expression沒有發生異常時通過測試; XCTAssertNoThrowSpecific(expression, specificException, format...)異常測試,當expression沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過; XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)異常測試,當expression沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過 復制代碼異步測試
舉一個最簡答的例子:進入到具體界面里的的方法:
//這里就直接定義一個字符串,假如出現異常,就直接拋出這個字符串XCTestExpectation *ex = [self expectationWithDescription:@"這里應該得進到訂單完成界面"];OrderPayDoneViewController *controller = [[OrderPayDoneViewController alloc] initWithNibName:nilbundle:nil];//這里就是訂單完成界面需要的屬性,可以隨便造假數據controller.orderId = @"這是訂單 ID";controller.isPaidSuccess = YES;controller.orderAmount = @"23456";controller.payMethod = @"Paypal";controller.displayAmount = @"$11.8.0";[CurrentViewController().navigationController pushViewController:controlleranimated:YES]; //下面這兩個方法就是XCTestExpectation的API[ex fulfill]; //這個方法不懂, 不寫也可以調用[self waitForExpectations:@[ex] timeout:1000000];//這個不調用不能跳轉 復制代碼單元測試
單元測試內容很多,還需要更多的研究,測試也很重要,對于開發對于測試都很重要,多多學習下單元測試吧。
后續
UI測試,可以自動進行測試。最基本的注冊登錄流程等常規,可以跑UI測試。。 內容比較多, 后續繼續研究補充。
總結
以上是生活随笔為你收集整理的Xcode 自带单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery 赋值时不触发change事
- 下一篇: zabbix' failed: [104