[Java测试]使用Cobertura度量代码覆盖率
2012-01-05 17:27
543 查看
昨天到今天看了一下这本书:JUnit in Action (second edition)的第5章,这一章讲了怎么使用Cobertura这个工具对测试时的代码覆盖率进行度量,这里我按照书里的描述,总结一下在Windows Server环境下的最简单方法。
首先,在http://cobertura.sourceforge.net/download.html页面中下载Cobertura,可以选择cobertura-1.9.4.1-bin.zip进行下载。另外,下载上面这本书的第5章源代码(我会在博客最后附上)。
其次,将待测代码(Calculator.java)和测试用例(TestCalculator.java)编译并将class文件存入"uninstrumented"这个文件夹中:
然后,将cobertura-1.9.4.1-bin.zip解压后的所有内容拷贝进文件夹"ch05-cobertura"中(为方便起见,不进行环境变量的设置),运行cobertura-instrument.bat:
上一步中将instrumented的class文件存入了"instrumented"文件夹中。
第四,运行下面的命令,以调用测试用例并用Cobertura来度量code coverage:
上面这个命令运行过后,可以看到在"cobertura.ser"文件,这个文件实际上就是测试结果,我们需要将其转为方便阅读的形式(如html),运行下面命令可以实现:
这样我们就在"reports"这个目录下生成了针对Calculator这个class文件,运行TestCalculator.class这个测试类,得到的测试覆盖率和其他结果,如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/16/5f8cd75b6cc3014fd8bbcde682e47167.gif)
通过上述几步,就能简单了解Cobertura的基本运行过程,其实很简单。后面如果学到更多关于Java测试的内容,会及时总结。
最后,将刚才几步所需的两个文件夹压缩包提供给大家,方便大家在不设置任何环境变量、不进行下载的情况下学习最基本步骤:
http://files.cnblogs.com/quyu/20120105-cobertura_example.zip
首先,在http://cobertura.sourceforge.net/download.html页面中下载Cobertura,可以选择cobertura-1.9.4.1-bin.zip进行下载。另外,下载上面这本书的第5章源代码(我会在博客最后附上)。
其次,将待测代码(Calculator.java)和测试用例(TestCalculator.java)编译并将class文件存入"uninstrumented"这个文件夹中:
javac -cp junit-4.6.jar -d uninstrumented src\*.java
然后,将cobertura-1.9.4.1-bin.zip解压后的所有内容拷贝进文件夹"ch05-cobertura"中(为方便起见,不进行环境变量的设置),运行cobertura-instrument.bat:
cobertura-instrument.bat --destination instrumented uninstrumented\Calculator.class
上一步中将instrumented的class文件存入了"instrumented"文件夹中。
第四,运行下面的命令,以调用测试用例并用Cobertura来度量code coverage:
java -cp junit-4.6.jar;cobertura.jar;instrumented;uninstrumented; -Dnet.sourceforge.cobertura.datafile=cobertura.ser org.junit.runner.JUnitCore TestCalculator
上面这个命令运行过后,可以看到在"cobertura.ser"文件,这个文件实际上就是测试结果,我们需要将其转为方便阅读的形式(如html),运行下面命令可以实现:
cobertura-report --format html --datafile cobertura.ser --destination reports src
这样我们就在"reports"这个目录下生成了针对Calculator这个class文件,运行TestCalculator.class这个测试类,得到的测试覆盖率和其他结果,如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/16/5f8cd75b6cc3014fd8bbcde682e47167.gif)
通过上述几步,就能简单了解Cobertura的基本运行过程,其实很简单。后面如果学到更多关于Java测试的内容,会及时总结。
最后,将刚才几步所需的两个文件夹压缩包提供给大家,方便大家在不设置任何环境变量、不进行下载的情况下学习最基本步骤:
http://files.cnblogs.com/quyu/20120105-cobertura_example.zip
相关文章推荐
- [Java测试]使用Cobertura度量代码覆盖率
- java里 使用apache-ant和cobertura测试代码覆盖率 环境变量配置
- 使用 Cobertura 和反射机制提高 Java 单元测试中的代码覆盖率
- cobertura代码测试覆盖率使用案例
- 使用Cobertura的命令行方式测试代码覆盖率
- 使用 Cobertura 和反射机制提高 Java 单元测试中的代码覆盖率
- 使用代码覆盖率分析度量测试的完整性
- 使用 Cobertura 和反射机制提高 Java 单元测试中的代码覆盖率
- 使用Cobertura做代码覆盖率测试
- Java单元测试Junit(六)使用Cobertura生成测试覆盖率报告
- 使用cobertura确定测试代码的覆盖率
- ant中使用cobertura分析测试用例的代码覆盖率
- 代码测试覆盖率Cobertura使用
- ant中使用cobertura分析测试用例的代码覆盖率
- Cobertura代码覆盖率测试
- 使用Cobertura统计JUnit测试覆盖率
- gcov代码覆盖率使用gcov完成代码覆盖率的测试
- java application 应用程序 使用JDBC proxool 两种方式连接数据库 测试程序代码
- Android UI测试框架Espresso以及代码覆盖率jacoco的使用
- 使用EclEmma统计代码的测试覆盖率 [ 光影人像 东海陈光剑 的博客 ]