简体   繁体   English

Cobertura如何与JUnit合作?

[英]How does Cobertura work with JUnit?

I can't understand how Cobertura cooperates with JUnit. 我无法理解Cobertura如何与JUnit合作。 As I understood cobertura modifies compiled byte code and inserts in this byte code its own commands. 据我所知,cobertura修改了编译的字节代码,并在此字节代码中插入了自己的命令。 Ok. 好。 After that we run Junit framework and give it our tests to run. 之后我们运行Junit框架并将其运行测试。 Could anyone explain at what points cobertura gets the information which of its commands were executed? 任何人都可以解释cobertura在什么点获取其命令执行的信息?

Cobertura uses ASM which is a general purpose bytecode manipulation and analysis framework. Cobertura使用ASM ,这是一个通用的字节码操作和分析框架。 On every line of java code there are 3 lines added to the existing classes to count things for the report it produces. 在每行java代码中,有3行添加到现有类中,以计算它生成的报告的内容。 When Cobertura is included in your classpath and configured correctly and you execute your unit tests, it will produce a datafile called cobertura.ser which is used to produce an xml or html report. 当Cobertura包含在您的类路径中并正确配置并执行单元测试时,它将生成一个名为cobertura.ser的数据文件,用于生成xml或html报告。

Basic usage: with Maven: http://www.mojohaus.org/cobertura-maven-plugin/usage.html 基本用法:使用Maven: http//www.mojohaus.org/cobertura-maven-plugin/usage.html

Cobertura monitors tests by instrumenting the bytecode with extra statements to log which lines are and are not being reached as the test suite executes. Cobertura通过使用额外的语句检测字节码来监视测试,以记录测试套件执行时哪些行和未到达的行。

Cobertura calculates coverage both by the number of lines tested and by the number of branches tested. Cobertura根据测试的行数和测试的分支数计算覆盖率。 For a first pass, the difference between these two is not hugely important. 对于第一次通过,这两者之间的差异并不是非常重要。 Cobertura also calculates the average McCabe's cyclomatic complexity for the class. Cobertura还计算了McCabe在该课程中的平均圈复杂度。

If using maven this can be configured in POM: 如果使用maven,可以在POM中配置:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <formats>
                <format>html</format>
                <format>xml</format>
            </formats>
        </configuration>
    </plugin>

If using ANT it can be configured with the taskdef statement in the build.xml file: 如果使用ANT,可以使用build.xml文件中的taskdef语句进行配置:

   <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

Reference for ant-cobertura integration can be found at https://github.com/cobertura/cobertura/wiki/Ant-Task-Reference 有关ant-cobertura集成的参考资料可以在https://github.com/cobertura/cobertura/wiki/Ant-Task-Reference找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM