為了提高ipa測試包的質(zhì)量芥牌,準(zhǔn)備在項目中集成自動化單元測試寸爆,于是對Xcode的自動化單元測試做了一些研究替废,準(zhǔn)備用幾篇博客與大家分享一下兴革。
- iOS自動化單元測試之Xcode自帶工具xcodebuild與xccov
- iOS自動化單元測試之fastlane中xcodebuild與xcov
- iOS自動化單元測試之生成覆蓋率網(wǎng)頁與發(fā)送釘釘消息
- iOS自動化單元測試之問題總結(jié)
預(yù)期達(dá)到的效果绎晃,可以設(shè)定單元測試的覆蓋率最低值,終端只輸入一條命令杂曲,就可以實現(xiàn)自動化單元測試及消息通知
1庶艾、釘釘消息,如果達(dá)到最低覆蓋率標(biāo)準(zhǔn)就可收到覆蓋率和ipa的地址擎勘,達(dá)不到只收到覆蓋率的通知咱揍。
2、網(wǎng)頁查看覆蓋率
一棚饵、單元測試準(zhǔn)備工作
1煤裙、創(chuàng)建一個含有Unit Tests的項目
- 前期創(chuàng)建項目時直接添加
創(chuàng)建項目是未添加,可后期添加噪漾。 xcode -> file -> new -> target -> iOS Unit Testing bundle
2硼砰、創(chuàng)建XCTestCase的測試文件
xcode -> file -> new -> file -> Unit Test case class
3、書寫單元測試
- (void)testPerson {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
[self.p sleep];
// [self.p eat];
NSString *name = [self.p getName];
XCTAssertTrue([name isEqualToString:@"BJT"],@"名字不相等");
}
4欣硼、單元測試target設(shè)置
info中添加tests题翰,options中勾選Gather coverage框。xcode9.2和xcode9.3位置不太一樣诈胜,下面的圖片是xcode9.3的配置
多target的時候注意一下豹障,每一個target都需要勾選Gather coverage框,在info里面添加tests焦匈,在manager scheme中勾選shared
5血公、執(zhí)行一下common + U可以看到Xcode自帶的覆蓋率,表示配置完成
二缓熟、xcodebuild生成.xccovreport
1坞笙、xcodebuild的終端命令
1、指定執(zhí)行單個文件的用例方法
bijietaodeMacBook-Pro:BJTUnitTestsDemo bijietao$ xcodebuild test -scheme BJTUnitTestsDemo -target BJTUnitTestsDemo:BJTPersonTests -only-testing:BJTUnitTestsDemoTests/BJTPersonTests -destination 'platform=iOS Simulator,name=iPhone 8 Plus,OS=11.3'
2荚虚、指定執(zhí)行單個文件的一個用例方法
bijietaodeMacBook-Pro:BJTUnitTestsDemo bijietao$ xcodebuild test -scheme BJTUnitTestsDemo -target BJTUnitTestsDemo:BJTPersonTests -only-testing:BJTUnitTestsDemoTests/BJTPersonTests/testPerson -destination 'platform=iOS Simulator,name=iPhone 8 Plus,OS=11.3'
3、執(zhí)行全部用例方法
bijietaodeMacBook-Pro:BJTUnitTestsDemo bijietao$ xcodebuild test -scheme BJTUnitTestsDemo -target BJTUnitTestsDemo -destination 'platform=iOS Simulator,name=iPhone 8 Plus,OS=11.3'
具體參數(shù)的講解可以看下面的官網(wǎng)描述
How do I run unit tests from the command line?
xcodebuild provides several options for running unit tests.
To build and run unit tests from the command line, execute the following command in Terminal:
xcodebuild test [-workspace <your_workspace_name>]
[-project <your_project_name>]
-scheme <your_scheme_name>
-destination <destination-specifier>
[-only-testing:<test-identifier>]
[-skip-testing:<test-identifier>]
To build unit tests without running them from the command line, execute the following command in Terminal:
xcodebuild build-for-testing [-workspace <your_workspace_name>]
[-project <your_project_name>]
-scheme <your_scheme_name>
-destination <destination-specifier>
To run unit tests without building them from the command line, execute any of the following command in Terminal:
xcodebuild test-without-building [-workspace <your_workspace_name>]
[-project <your_project_name>]
-scheme <your_scheme_name>
-destination <destination-specifier>
[-only-testing:<test-identifier>]
[-skip-testing:<test-identifier>]
xcodebuild test-without-building -xctestrun <your_xctestrun_name>.xctestrun
-destination <destination-specifier>
[-only-testing:<test-identifier>]
[-skip-testing:<test-identifier>]
2籍茧、查看效果
- 終端會看到運(yùn)行效果版述,及test succeeded的成功提示
- 可以通過文件路徑,查找覆蓋率文件
在 ~/Library/Developer/Xcode/DerivedData/ 路徑下的 Logs/Test中可以查看代碼覆蓋率文件.xccovreport和.xccovarchive寞冯。
在Logs/Test目錄中渴析,有擴(kuò)展名為.xccovreport的覆蓋率報告文件晚伙,和擴(kuò)展名為.xccovarchive的覆蓋率數(shù)據(jù)歸檔文件。蘋果應(yīng)用手冊中注釋說明如下:“覆蓋率報告包含有每個target俭茧,源文件咆疗,以及具有覆蓋信息功能/方法的線性覆蓋百分比等內(nèi)容。覆蓋率數(shù)據(jù)歸檔文件包含有每個文件的原始執(zhí)行次數(shù)“母债。
三午磁、xccov生成覆蓋率(支持最低版本Xcode9.3)
因為.xccovreport 與 .xccovarchive,這些文件并不適用于用戶查閱毡们,這就是我們需要使用xccov來查看這些文件迅皇,并以完美的樣式顯示報告。
- 從終端查看代碼覆蓋率報告衙熔;
- 從代碼覆蓋率報告中解析出JSON樣式數(shù)據(jù)登颓;
- 列出已生成代碼覆蓋率的所有文件;
- 查看一個特定文件的代碼覆蓋率報告红氯。
1框咙、查看報告 -Default
我們可以以默認(rèn)格式查看代碼覆蓋率報告,這種格式并不是很好痢甘,但根據(jù)Apple的說明喇嘱,它是用戶可讀的。在我們的演示項目中产阱,我們可以使用以下命令生成報告:
$ xcrun xccov view Logs/Test/*.xccovreport
執(zhí)行上面命令時婉称,需要先前往DerivedData的項目文件夾下,或者寫全路徑(/Users/bijietao/Library/Developer/Xcode/DerivedData/BJTUnitTestsDemo-dycoddwyqpwfaqeybquzsxriqrfn/Logs/Test/*.xccovreport )构蹬。
效果如下:
2王暗、查看報告 - JSON
xccov真正強(qiáng)大的地方,就是能夠以JSON格式生成代碼覆蓋率報告庄敛。我們可以使用以下命令來生成JSON格式的報告:
$ xcrun xccov view Logs/Test/*.xccovreport --json
效果如下:
3俗壹、列出所有文件
$ xcrun xccov view --file-list Logs/Test/*.xccovreport --json
4、特定文件的代碼覆蓋率
$ xcrun xccov view --file /Users/bijietao/Desktop/Unittest/5/BJTUnitTestsDemo/BJTUnitTestsDemo/AppDelegate.m Logs/Test/*.xccovarchive