running junits and cobertura with maven
Asked Answered
J

3

23

In our project, we run both junits and cobertura using maven. The problem I am facing is that,

  1. junit test cases are running twice, once before the jar creation process and then once again for generating cobertura coverage reports. When running cobertura and junits with ant, we run junits only once since, cobertura runs along with junits. Is there a way to configure the above case with maven. I know we can use "maven.test.skip" property to skip junits. But when I do this, I am not able to see junit xml & html file reports.
  2. Also, in maven how to configure junits to run in batch or parallel ?

thank you!

Joanjoana answered 9/4, 2009 at 6:4 Comment(1)
your second question is really a separate issue, cna seems to be reposted here: #424127Shahjahanpur
G
23

While I cannot find the exact page anymore, I recently read a discussion of why running the tests twice is considered a good idea. The key issues cited were around the effects of the Cobertura byte code alteration on the accuracy of your tests. In certain cases the timing of your code execution might be important, the byte code alteration can cause tests that fail in JUnit to pass when run only in Cobertura and vice versa. For this reason, it was recommended that the tests be allowed to execute twice. Most of the examples cited were around multi-threaded behaviors, but I imagine that there could be other cases were the byte code alteration can cause issues in your tests. Having the tests execute both ways provides you with baseline results and also reduces the chances of sending you on a wild goose chase if Cobertura is in fact altering test success.

Guggle answered 15/5, 2009 at 12:16 Comment(5)
This is a great point. Another thing I ran into one time is that because of the byte-code re-writing, if your code uses reflection a lot, it can cause issues. For example, if you have a utility that extracts fields from a class, and your unit test asserts that an example class has three fields, this test will actually fail when you run it with code coverage.Desouza
Not to mention the fact that tests without instrumentation run a lot faster.Episode
@DavidValeri: Could you please provide a link to the discussion thread you refer (just edit our post)?Pyrrha
The fact that Cobertura alters the bytecode can be referenced on their website cobertura.sourceforge.net/introduction.html .Reenareenforce
I don't see anything good in running tests twice. It's a clear indication of bad tests, bad tools or a bad configuration.Helyn
E
1

Try adding cobertura as a compile scope reference. And post the relevant parts of your pom.

Exoskeleton answered 13/4, 2009 at 18:56 Comment(1)
Wouldn't having as a dependency (with compile scope) only be helpful if your project is somehow trying to build something on top of cobertura? This is probably not what Ajay was looking for.Desouza
E
1

This happens because the reporting execution requires the test execution so it can create the reports. If there were a "site-only" goal on the site plugin that didn't have the @requiresDependencyResolution test annotation, it could be bound to the project's prepare-package phase and your reports would be generated without the tests running twice.

Unfortunately there seems to currently be no such goal (see my question on the subject). See my answer to the question for details of a workaround.

Ethylene answered 30/7, 2009 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.