xcode支持統計手動測試的代碼覆蓋率,對於降低漏測率有顯著的幫助,最近想要嘗試一下這方面的調研,記錄一下自己的實現方法。
持續更新中。。。。。。
第一步:安裝ruby環境
mac電腦一般不會自帶ruby,需要自己安裝,如果已經安裝了ruby的可以忽略
1.安裝rvm:$ curl -L https://get.rvm.io | bash -s stable
2.設載入rvm:$ source ~/.rvm/scripts/rvm
3.檢查是否安裝成功:$ rvm -v
4.查看ruby版本:$ rvm list known
5.安裝ruby:$ rvm install 2.4
(注意:安裝的目標文件夾當前用戶必須有write權限,homebrew也要更新到最新版)
6.查看安裝的ruby版本:$ rvm list
7.將安裝的ruby設爲系統默認版本:$ rvm 2.0.0 —default
第二步:安裝cocoaPods
1.命令:sudo gem install cocoapods
(注意:這裏可能需要電腦翻牆,有的人是使用了國內的源:http://code4app.com/article/cocoapods-install-usage)
第三步:將XcodeCoverage插入到你的項目中
1.cd到工程路徑下,新建一個Podfile文件: vim Podfile
2.在文件中加入一行:pod 'XcodeCoverage', ‘~>1.0'
第三步:將XcodeCoverage插入到你的項目中
1.cd到工程路徑下,新建一個Podfile文件: vim Podfile
2.在文件中加入一行:pod 'XcodeCoverage', ‘~>1.0'
3.運行:pod install
這時可能會遇到一個問題,報錯:error: RPC failed; curl 56 SSLRead() return error -36
這時因爲安裝了大於1個Xcode導致的,需要明確一下Xcode路徑
終端輸入:sudo xcode-select -switch /……./Xcode.app/Contents/Developer
之後重新執行:pod install
這時還可能會遇到錯誤:The dependency `XcodeCoverage (~> 1.0)` is not used in any concrete target.原因是最新版本的pop有語法變動還有SDK版本沒有做兼容。
解決方法:vim Podfile
插入內容:target 'TestApp' do
pod 'XcodeCoverage', '~>1.0'
end
platform :ios, '10.2'
保存退出重新執行:pod install
第四步:將待測target改成debug模式
如下圖,需要改2個地方
這裏解釋一下target的含義,現在的app都會有不同的版本迭代如:1.0 2.0等,由於現在我使用的app是一個demo,所以只有一個target,可以在上圖中的黃色iconTestAPP中切換target。
此時再檢查一下app能否正確在模擬器上運行,我在這裏遇到了報錯:ld: library not found for -lPods。原因是需要要打開後綴名爲xcworkspace的文件運行。IOS項目當運用了cocopods引入第三方時不能打開原樣式的文件
第五步:項目中插入代碼
在app項目中找到APPDelegate.m文件,修改如下部分:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
#if NT_COVERAGE
#if !TARGET_IPHONE_SIMULATOR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
setenv("GCOV_PREFIX", [documentsDirectory cStringUsingEncoding:NSUTF8StringEncoding], 1);
setenv("GCOV_PREFIX_STRIP", "13", 1);
#endif
extern void __gcov_flush(void);
__gcov_flush();
#endif
}
第六步:在在模擬器中操作
1.build&run待測app,在模擬器中操作,點擊Home鍵。IOS模擬器的HOME鍵:command+shift+H
2.cd 進入 項目/Pod/XcodeCoverage 運行:./getcov —show
就可以自動生成測試報告
注:這個時候可能會遇到以下問題
1.WARNING: no .da files found in /Users。。。。。。。 - skipping!
2.lcov: ERROR: no valid records found in tracefile coverage.info
原因主要可能有2點:
1.lcov-1.12 並不是最新版,只有1.12纔開始兼容了ios10
下載鏈接:https://github.com/daihongchao/XcodeCoverage
2.linux環境導致的不適配,解決方法:
修改/envcov.sh:
LCOV()
{
"${LCOV_PATH}/lcov" “$@"
}
修改/getcov
"${LCOV_PATH}/genhtml" --ignore-errors source --output-directory . “${LCOV_INFO}"
問題都解決之後就可以查看到剛纔操作的代碼覆蓋率報告了。
參考鏈接:
https://github.com/jonreid/XcodeCoverage/blob/master/README.md
https://testerhome.com/topics/6644
https://testerhome.com/topics/8468