Can my build stipulate that my code coverage never get worse?

流过昼夜 提交于 2019-12-03 14:52:36

Yes. Which coverage tool are you using?

The Cobertura plugin for Hudson definitely supports this. On the project configuration screen you can specify thresholds.

Alternatively, you can make Ant fail the build (rather than Hudson), by using the cobertura-check task.

EDIT: I'm not sure you can do precisely what you are asking for. Even if you could, it could prove problematic. For example, assume you have an average coverage of 75% but for one class you have coverage of 80%. If you remove that 80% class and all of its tests, you reduce the overall coverage percentage even though none of the other code is any less tested than previously.

This is kind of a hack, but we use it for similar reasons with Findbugs and Checkstyle. You can set up an Ant task to do the following (this can be split out into multiple tasks, but I'm combining them for brevity):

  1. Run tests with coverage
  2. Parse the coverage results and get the coverage percentage
  3. Read tmp/lastCoverage.txt from last build (see step #5a)
  4. Compare the current coverage percentage with the percentage read from lastCoverage.txt
    1. If percentage DIDN'T decrease, write the new percentage over the contents of tmp/lastCoverage.txt
    2. If percentage DID decrease, keep the original file and echo "COVERAGE FAILURE" (with ant's echo task).

Note that steps 2 through 5 don't necessarily need to be done with native Ant tasks - you could use something like Ant's javac task to run a Java program to do this for you.

Then, configure Hudson:

  • Under "Source code management", make sure "Use Update" is checked. This will allow your lastCoverage.txt file to be retained between builds. Note that this could be problematic if you really, really need things to be cleaned between builds.
  • Use the Hudson Text Finder plugin with a regular expression to search for "COVERAGE FAILURE" in the build output (make sure that "Also search console output" is checked for the plugin). The text finder plugin can mark the build unstable.

You can obviously replace things like the file name/path and console output to whatever fits within the context of your build.

As I mentioned above, this is rather hacky, but it's probably one of the few (only?) ways to get Hudson to compare things in the previous build to the current build.

Another approach would be to use the Sonar plugin for Hudson to maintain trending of coverage over time, and make it easier to assimilate and analyze results. It will also show coverage in context of other measures, such as checkstyle and pmd

Atlassian's Clover supports what you want. Have a look at the clover-check Ant task, specifically the historyDir attribute.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!