一: KIF 三方庫的配置
今天的廣州天氣還不錯,原本想試試UI測試的,前幾天也了解到很多公司都在用 KIF 這這三方框架!!今天也就試着做做,可就跪在了這個安裝上,我用cocopods 導入了 KIF(不用cocopods 的方法可以去 git 具體自己看看怎么配置這是Git地址:GitKIF ),現在是已經更新到 3.4.2 這個版本了,去git上看到兩天前作者還在更新。說明KIF是沒問題還在更新維護是可用的。結果。。。Run~
dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /Users/mxsm/Library/Developer/CoreSimulator/Devices/B6E8A337-2678-4C4B-9DF1-C97586418141/data/Containers/Bundle/Application/6F05F8A0-A445-49F9-B222-570CA7C49EB6/sinatest.app/sinatest Reason: image not found
導演,你出來,這他么是什么鬼!!
開玩笑,這個問題其實我也糾結了許久,上網搜了許多許多的方法,嘗試了很多次!結果。。還是漏掉了要配置寫東西!下面是官方英文版本,英語好的仔細閱讀,不好的,請閉眼一秒鍾,我們去下面看看。。。
Final Test Target Configurations
You need your tests to run hosted in your application. Xcode does this for you by default when creating a new testing bundle target, but if you're migrating an older bundle, follow the steps below.
First add your application by selecting "Build Phases", expanding the "Target Dependencies" section, clicking on the "+" button, and in the new sheet that appears selecting your application target and clicking "Add".
Next, configure your bundle loader. In "Build Settings", expand "Linking" and edit "Bundle Loader" to be $(BUILT_PRODUCTS_DIR)/MyApplication.app/MyApplication
where MyApplication is the name of your app. Expand the "Testing" section and edit "Test Host" to be $(BUNDLE_LOADER)
. Also make sure that "Wrapper Extension" is set to "xctest".
The last step is to configure your unit tests to run when you trigger a test (⌘U). Click on your scheme name and select "Edit Scheme…". Click on "Test" in the sidebar followed by the "+" in the bottom left corner. Select your testing target and click "OK".
好,我把這整個給大家翻譯成簡單粗暴的形似。。(其實我都是百度翻譯摸索的。勞之看不懂!)
一: 項目名Tests對象 (項目名+Tests)---> Build Phase ---> Target Dependencies ---> "+" --->"項目的Tests文件"(去百度一下這個 tests 文件 和UItests 文件有什么區別)
二: 項目名Tests對象 ---> Build Settings ---> Linking(直接搜)---> Bundle Loader 填寫"$(BUILT_PRODUCTS_DIR)/項目名稱.app/項目名稱"
三: 項目名Tests對象 ---> Build Settings ---> Wrapper Extension (直接搜)設置成 "xctest"
四: 點擊你 RUN 按鈕隔壁的隔壁的項目target ---> Edit Scheme... ---> Test 看看里面有沒有你要測試的項目,沒有就添加(一般都有感覺)!
這時候的配置完成了,Command + U,沒有問題,以為可以了,可當自己寫了一個繼承 KIFTestCase 的文件的時候,問題又有了!說<KIF/KIF.h>頭文件找不到,其實這個也正常,你現在是在 Tests 對象(這個項目里就是ZXDNLLTestTests)里面。不是在你得項目target對象里面,你得重新配置一下。下面是在你得 tests 對象里面的配置,這點地方填寫的具體內容,從你的target對象(這個項目里面就是 ZXDNLLTest,怕有同學還是不明白!)里面粘貼復制就行了!
以上這些配置了之后,我的 KIF 也就可以了。創建 LoginTestCasse ,再說一次,它是繼承 KIFTestCase 如圖:
二:具體測試代碼詳解
// // LoginTestCase.m // ZXDNLLTest // // Created by mxsm on 16/4/28. // Copyright © 2016年 mxsm. All rights reserved. // #import "LoginTestCase.h" #import "AFNetworking.h" @implementation LoginTestCase /* beforeAll 是一個在所有測試運行之前被調用一次的特殊方法。你可以為你這里運行的測試設置任何實體變量和初始化條件. tester 對象是指定的 KIFUITestActor 類的一個縮略名稱。這個類包含了模擬用戶動作的方法,包括觸摸和滑動. tapViewWithAccessibilityLabel 這也許是最常被用到的測試動作方法。正如其名稱所顯示的,它可以在給定的輔助標簽模擬在視圖上的觸擊。在大多數情況下,輔助標簽和可視的文本標簽(例如按鈕組件)是配套的。否則你就需要手動設置輔助標簽. 一些控件,諸如 UISwitch,更加復雜,需要比簡單的觸擊更復雜的步驟來觸發。 KIF 提供了一個特殊的 setOn:forSwitchWithAccessibilityLabel: 方法來改變一個切換的狀態. */ - (void)beforeAll { //// [tester tapViewWithAccessibilityLabel:@"Settings"]; [tester setOn:YES forSwitchWithAccessibilityLabel:@"Debug Mode"]; [tester tapViewWithAccessibilityLabel:@"Clear History"]; [tester tapViewWithAccessibilityLabel:@"Clear"]; } // 這里的測試方法使用 test開頭的,后面的自己寫,你要用command+U 的形式運行的話,它的運行的順序就是除了test之外的后面的按字母排序的方式運行,比如下面的這個方法,出test之外第一個字母是Z,你要是又命名一個方法,testyou ,它是y ,他就會先運行。當然你也可以一個方法一個方法的運行,點擊方法前面的框框就OK了! -(void)testzhangxuone { // 可以在這里添加自己需要的測試的內容! int a = 0; if (a==0) { NSLog(@"you are successful"); } } /* 下面是在簡書找到的一篇關於單元測試的一個網絡請求的測試,其實大家可以對比一下KIF !!! KIF和蘋果自帶的UI測試兩者本質是一樣的! 不過下面的請求是 AFNetworking 3.0 以前的版本的,大家用還是用3.0之后的吧! AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; 關於3.0 之后的給大家一個連接,可以去看看!====== http://www.jianshu.com/p/047463a7ce9b */ // 下面方法的原文鏈接 ============= http://www.jianshu.com/p/8bbec078cabe //-(void)testRequest{ // // 1.獲得請求管理者 // AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; // mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil]; // // 2.發送GET請求 // [mgr GET:@"http://www.weather.com.cn/adat/sk/101110101.html" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // NSLog(@"responseObject:%@",responseObject); // XCTAssertNotNil(responseObject, @"返回出錯"); // self.stAlertView = [[STAlertView alloc]initWithTitle:@"驗證碼" message:nil textFieldHint:@"請輸入手機驗證碼" textFieldValue:nil cancelButtonTitle:@"取消" otherButtonTitle:@"確定" cancelButtonBlock:^{ // //點擊取消返回后執行 // [self testAlertViewCancel]; // NOTIFY //繼續執行 // } otherButtonBlock:^(NSString *b) { // //點擊確定后執行 // [self alertViewComfirm:b]; // NOTIFY //繼續執行 // }]; // [self.stAlertView show]; // } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // NSLog(@"error:%@",error); // XCTAssertNil(error, @"請求出錯"); // NOTIFY //繼續執行 // }]; // WAIT //暫停 //} @end
網上有一篇翻譯的國外的文章,好多好多寫 KIF 的直接是把那篇給通篇粘貼復制了,鏈接還是給大家,你理解了KIF,知道他是做什么的,也導入成功了,你可以去看看那篇文章,對自己肯定也有幫助,鏈接給大家! KIF學習鏈接
還有一點注意,我代碼里面給的 http://www.jianshu.com/p/8bbec078cabe 這個鏈接里面,還有幾個鏈接不錯的,有更進一步學習單元測試的視頻,大家進去自己看就行了!