Freemarker自动生成代码的Cobertura代码覆盖率 [英] Cobertura code coverage for Freemarker auto-generated code
问题描述
我正在尝试在一个项目中设置Cobertura代码覆盖范围,该项目包括使用Freemarker(明确地使用fmpp maven插件)从模板创建的自动生成的代码。
I am trying to set up Cobertura code coverage on a project which includes auto-generated code, created from templates using Freemarker (explicitly, using the fmpp maven plugin).
然后对这些自动生成的类进行单元测试。
I then have unit tests for those auto-generated classes.
但是,Cobertura在计算代码覆盖率时并未考虑这些单元测试。自动生成的类会出现在报告中,但是这些类的覆盖率始终为0%。
However, these unit tests are not being considered by Cobertura when calculating code coverage. The auto-generated classes appear in the report, but the coverage on those classes is always 0%.
首先,我缺少Cobertura的某些配置吗?
First, is there some configuration for Cobertura that I'm missing?
此 SO问题似乎一直在问类似的问题,但可接受的答案是:
This SO question appears to have been asking a similar question, but the accepted answer is that:
不应测试生成的代码,也不应对其进行测试。在代码覆盖率指标中使用。
Generated code should not be tested and should not be used in code coverage metrics.
这对我来说似乎不正确-我认为我应该测试生成的代码(两者都要测试)生成的类和模板),我想知道生成的代码的代码覆盖率。
This does not seem right to me - I think I should be testing the generated code (both to test the generated classes and the templates), and I want to know how the code coverage for this generated code.
因此,第二,有充分的理由说明为什么生成的代码不应
So, second, is there a good reason why generated code shouldn't be tested?
编辑:提到我正在使用cobertura-maven-plugin使用cobertura也很重要。因此,我不确定问题是否出在cobertura或maven插件(或其配置...)。
It's also relevant to mention that I am using cobertura using the cobertura-maven-plugin. As such, I'm not sure if the problem is with cobertura or the maven-plugin (or my configuration thereof...)
N.b。明确地说,我并不是在问如何自动生成测试类。这些是手动编写的,以测试从模板创建的类。
N.b. to be clear, I am not asking about auto-generating the test classes. These are manually written, to test the classes created from templates.
推荐答案
这是报告我所学知识的部分答案到目前为止...
This is a partial answer to report what I've learnt so far...
首先,我了解了cobertura的工作原理,似乎如下:
First, I've had a look at how cobertura works, and it seems to be as follows:
- 对项目java类进行了修改,以在整个代码中包含对TouchCollector.touch(...)(和类似方法)的调用,并进行编译。 (这是检测任务。)
- 测试是在这些cobertura编译类上运行的,TouchCollector记录了在测试中达到了哪些项目类行。此信息记录在cobertura.ser数据文件中。
- cobertura /报告任务读取cobertura.ser数据文件并生成html报告。
因此,我首先想到的是,自动生成的类没有得到适当的检测。但是,在反编译这些cobertura类文件之后,我可以确认正常和自动生成的类均已正确检测。
So, my first thought was that the auto-generated classes were not being instrumented properly. However, after decompiling these cobertura class files, I can confirm that both normal and auto-generated classes have been instrumented correctly.
此外,我可以确认所有测试-
Also, I can confirm that all the tests - including the tests of the auto-generated classes are being run.
然后,我的下一个问题是为什么在运行测试时,不会自动生成类感动。进一步的调查表明,在编译测试类(即测试-编译)时,自动生成的类会添加到项目/目标/测试类中。打印自动生成的类的文件位置的简单测试(例如 System.out.println(MyAutoClass.class.getResource( MyAutoClass.class));
)确认由cobertura运行测试时,它将使用test-classes文件夹中的自动生成的类,而不是已检测的cobertura编译的类。
Then, my next question is why, when the tests are run, the auto-generated classes are not being 'touched'. Further investigation showed that when the test classes were being compiled (i.e. test-compile), the auto-generated classes are added to project/target/test-classes. A simple test to print the file location of an auto-generated class (e.g. System.out.println(MyAutoClass.class.getResource("MyAutoClass.class"));
) confirmed that when the tests are run by cobertura, it uses the auto-generated classes in the test-classes folder, and not the cobertura compiled classes that had been instrumented.
然后,下一个问题是如何防止将这些类添加到test-classes文件夹中?好吧,一种方法是从编译中排除自动生成的类。这可以通过仅包含Test类来完成,例如:
Then, the next question is how to prevent these classes being added to the test-classes folder...? Well, one approach is to exclude the auto-generated classes from being compiled. This can be done by only including the Test classes - e.g.:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>x.y.z</version>
<configuration>
<testIncludes>
<testInclude>**/*Test.java</testInclude>
</testIncludes>
</configuration>
</plugin>
或者应该可以排除自动生成的类(首选)-
Or it should be possible to exclude the auto-generated classes (which would be preferred) - something along the lines of:
<testExcludes>
<testExclude>**/generated-sources/fmpp/**/*.java</testExclude>
</testExcludes>
但是,这没用,我不太确定如何使它起作用
However, this didn't work, and I'm not quite sure how to get this to work.
另一种方法是将所有自动生成的类移到单个程序包中,然后可能会发生以下情况:
An alternative could be to move all the auto-generated classes into a single package, and then something like the following could be possible:
<testExcludes>
<testExclude>com/organisation/project/auto/**/*.java</testExclude>
</testExcludes>
最后,我只使用了 * Test.java
文件
这篇关于Freemarker自动生成代码的Cobertura代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!