什么是代碼覆蓋率?
代碼覆蓋率是對整個測試過程中被執(zhí)行的代碼的衡量,它能測量源代碼中的哪些語句在測試中被執(zhí)行吵瞻,哪些語句尚未被執(zhí)行运嗜。
為什么要測量代碼覆蓋率?
眾所周知,測試可以提高軟件版本的質(zhì)量和可預測性痹扇。但是铛漓,你知道你的單元測試甚至是你的功能測試實際測試代碼的效果如何嗎?是否還需要更多的測試?
這些是代碼覆蓋率可以試圖回答的問題溯香。總之浓恶,出于以下原因我們需要測量代碼覆蓋率:
● 了解我們的測試用例對源代碼的測試效果
● 了解我們是否進行了足夠的測試
● 在軟件的整個生命周期內(nèi)保持測試質(zhì)量
注:代碼覆蓋率不是靈丹妙藥玫坛,覆蓋率測量不能替代良好的代碼審查和優(yōu)秀的編程實踐。
通常包晰,我們應該采用合理的覆蓋目標湿镀,力求在代碼覆蓋率在所有模塊中實現(xiàn)均勻覆蓋,而不是只看最終數(shù)字的是否高到令人滿意杜窄。
舉例:假設代碼覆蓋率只在某一些模塊代碼覆蓋率很高肠骆,但在一些關鍵模塊并沒有足夠的測試用例覆蓋,那樣雖然代碼覆蓋率很高塞耕,但并不能說明產(chǎn)品質(zhì)量就很高蚀腿。
關于Cobertura
Cobertura可以理解為一種測試覆蓋率報告方案,通過Cobertura + Maven + Jenkins來實現(xiàn)測試覆蓋率的展示扫外。
如何使用Cobertura
1莉钙、pom中添加依賴
? <!-- 在clean時把老的ser文件也清理掉 -->
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-clean-plugin</artifactId>
? ? ? ? ? ? ? ? <version>2.4.1</version>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <filesets>
? ? ? ? ? ? ? ? ? ? ? ? <fileset>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <directory>.</directory>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <includes>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <include>**/*.ser</include>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </includes>
? ? ? ? ? ? ? ? ? ? ? ? </fileset>
? ? ? ? ? ? ? ? ? ? </filesets>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.codehaus.mojo</groupId>
? ? ? ? ? ? ? ? <artifactId>cobertura-maven-plugin</artifactId>
? ? ? ? ? ? ? ? <version>2.7</version>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <formats>
? ? ? ? ? ? ? ? ? ? ? ? <format>html</format>
? ? ? ? ? ? ? ? ? ? ? ? <format>xml</format>
? ? ? ? ? ? ? ? ? ? </formats>
? ? ? ? ? ? ? ? ? ? <aggregate>true</aggregate> <!-- 表示會聚合不同子module下的測試報告 -->
? ? ? ? ? ? ? ? ? ? <check/>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
2、通過命令行生成報告文件
輸入如下命令:
mvn cobertura:cobertura
它會在每個模塊以及根目錄的target下分別產(chǎn)生.ser文件以及相應覆蓋率報告文件筛谚。
3磁玉、覆蓋率報告
覆蓋率的html報告如下:
4、其他定制需要
因?qū)嶋H場景需要驾讲,排除某些方法蚊伞,需要調(diào)整如下配置:
<plugin>
? ? <groupId>org.apache.maven.plugins</groupId>
? ? <artifactId>maven-surefire-plugin</artifactId>
? ? <version>2.18.1</version>
? ? <configuration>
? ? ? ? <includes>
? ? ? ? ? ? <include>**/*TestJMock.java</include>
? ? ? ? </includes>
? ? ? ? <excludes>
? ? ? ? ? ? <exclude>**/*Test.java</exclude>
? ? ? ? </excludes>
? ? </configuration>
</plugin>
如果要對測試覆蓋率進行控制,一定要達到指定標準呢吮铭,做如下配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
? ? <formats>
? ? ? ? <format>html</format>
? ? ? ? <format>xml</format>
? ? </formats>
? ? <aggregate>true</aggregate> <!-- 表示會聚合不同子module下的測試報告 -->
? ? <check>
? ? ? ? <branchRate>0</branchRate>
? ? ? ? <lineRate>0</lineRate>
? ? ? ? <haltOnFailure>true</haltOnFailure>
? ? ? ? <totalBranchRate>0</totalBranchRate>
? ? ? ? <totalLineRate>0</totalLineRate>
? ? ? ? <packageLineRate>0</packageLineRate>
? ? ? ? <packageBranchRate>0</packageBranchRate>
? ? ? ? <regexes>
? ? ? ? ? ? <regex>
? ? ? ? ? ? ? ? <pattern>com.company.mode.services.*</pattern>
? ? ? ? ? ? ? ? <branchRate>60</branchRate>
? ? ? ? ? ? ? ? <lineRate>80</lineRate>
? ? ? ? ? ? </regex>
? ? ? ? </regexes>
? ? </check>
</configuration>
</plugin>
通過mvn cobertura:check即可對覆蓋率進行校驗
這個Maven插件的一些缺陷:
1时迫、不支持lambda表達式,在mvn命令的執(zhí)行中會報錯谓晌,即使在通過git倉庫下載最新Cobertura代碼自己打包生成的jar包掠拳,雖然沒有報錯,但是依然不能正常產(chǎn)生報告
2纸肉、根目錄下的測試覆蓋率文件似乎只是隨機選了底下一個module的覆蓋率溺欧,沒有按照我們的設想來:將所有子module的測試覆蓋率匯總。
與Jenkins集成
安裝Cobertura插件
設置 Pre Steps
添加構建后操作
生成覆蓋率報告
所有的配置都好了柏肪,點擊立即構建姐刁,即可生成相應的測試覆蓋率報告