Jenkins提供了很多插件,用于展示代碼測試、檢測后的報(bào)告。
最近當(dāng)前項(xiàng)目需要完成這些插件的配置,于是便做了一些了解和探索,在這里簡單羅列一些插件的作用及用法,以備查閱。
項(xiàng)目是用Swift開發(fā)的,相關(guān)插件的配置也是針對Swift。
Code Coverage
代碼覆蓋率用來衡量,當(dāng)你跑測試的時(shí)候,你有多少代碼沒有覆蓋到。
在iOS開發(fā)中,已經(jīng)為我們提供了工具,用于生成代碼覆蓋率數(shù)據(jù)。
項(xiàng)目名"PROJECT",scheme名為"SCHEME"
Xcodebuild
執(zhí)行測試命令,設(shè)置-enableCodeCoverage為YES表明需要生成代碼覆蓋率數(shù)據(jù),
xcodebuild test \
-workspace PROJECT.xcworkspace \
-scheme SCHEME \
-configuration Debug \
-destination 'platform=iOS Simulator,name=iPhone 8' \
-enableCodeCoverage YES \
執(zhí)行完成后,會在derivedData中生成coverage.profdata,里面存放的就是代碼覆蓋率的數(shù)據(jù)。
針對這樣一份數(shù)據(jù),我們并不認(rèn)識,所以需要借助另一個(gè)工具來解析。
Slather
Slather
Generate test coverage reports for Xcode projects & hook it into CI.
文檔中詳細(xì)地描述了slather能夠做的事情,目前對我來說,它最大的作用就是將coverage.profdata轉(zhuǎn)換成cobertura類型的xml文件。
關(guān)于Cobetrura,我的理解是,它是一個(gè)生成代碼覆蓋率的工具的java工具,它生成的結(jié)果是xml,而jenkins上有相應(yīng)插件能夠展示這個(gè)xml。*
對于我們而言,我們這里讓slather幫助我們生成了這個(gè)能夠在jenkins上展示的xml,這就足夠了。*
執(zhí)行slather命令,
slather coverage -x \
--scheme SCHEME \
--workspace PROJECT.xcworkspace \
--binary-basename DISPLAY_NAME \
PROJECT.xcodeproj
-x:生成cobertura格式的xml
--binary-basename: Display name
這里遇到了兩個(gè)坑,
- 官方使用了
-s進(jìn)行簡單的輸出,于是我使用-s -x輸出文檔,但xml沒有生成,只是用-x解決這個(gè)問題。 - 不添加
--binary-basename,導(dǎo)致提示找不到數(shù)據(jù)。
在Jenkins的Publish Cobertura Coverage Report中填入相關(guān)xml文件即可。
SLOCCount / cloc
SLOCCount和cloc都是用來檢測源碼的行數(shù)的,它們很類似,生成的xml也能夠通用。
在SLOCCount的Jenkins插件的描述中提到了一句:
Cloc (Count Lines of Code) is a tool similar to SLOCCount. It provides output to a XML file that can be simply transformed to the SLOCCount format and used in this plugin. Cloc is written in Perl and should be better portable than SLOCCount.
項(xiàng)目中,我實(shí)際上用的是cloc,
cloc --by-file --xml --out=OUTPUT_PATH DIRECTORY_PATH
Jenkins中使用Publish SLOCCount analysis results來展示,同樣填入xml路徑即可。
Swiftlint
Swiftlint這個(gè)工具相對常用很多,為了生成checkstyle類型的文件,
在.swiftlint.yml配置文件,添加如下配置,
reporter: "checkstyle"
然后根據(jù)需求執(zhí)行swiftlint相關(guān)命令就行
最后在Jenkins的Publish Checkstyle analysis results中填入xml文件即可。
PMD
PMD這次沒有用到,但我也看了一下它的overview,感覺跟Swiftlint很類似,但是多了CPD(copy-paste-detector),以后用到再詳細(xì)了解。