我的构建能否规定我的代码覆盖率永远不会变得更糟?

发布于 2024-07-10 14:23:10 字数 449 浏览 9 评论 0原文

我正在使用 hudson CI 来管理一个直接的 java web 项目,使用 ant 来构建。

我想强制要求单元测试覆盖率永远不会比以前的版本差,从而确保任何新代码始终经过测试,或者至少覆盖率不断提高。

有没有 hudson 插件可以这样工作?

编辑:我目前正在使用艾玛,但愿意切换到另一个覆盖应用程序。

另外,作为澄清,我已经看到了一些 Hudson 插件中的阈值,但这并不完全是我想要的。 例如,我想要的是,如果 Build #12 的覆盖率总体为 46%,并且有人以 45% 的覆盖率签入 Build #13,则构建将会中断。

我想这样做的原因是我的代码库测试覆盖率较低。 我们没有时间回去追溯编写单元测试,但我想确保覆盖范围不断变得更好。

更新:丹指出了我的计划中的一个极端情况,这肯定会成为一个问题。 我想我需要重新考虑这是否是一个好主意。

I am using hudson CI to manage a straight java web project, using ant to build.

I would like to mandate that the unit test coverage never be worse than the previous build, thereby making sure any new code is always tested, or at least the coverage is continually improving.

Is there a hudson plugin that works this way?

Edit: I am currently using Emma, but would be willing to switch to another coverage app.

Also, as a clarification, I've seen the thresholds in some Hudson plugins, but that's not exactly what I'm after. For example what I'd like is that if coverage for Build #12 was 46% overall, and someone checked in Build #13 with 45% coverage, the build would break.

The reason I want to do this, is that I have a codebase with low test coverage. We don't have time to go back and retroactively write unit tests, but I'd like to make sure that the coverage keeps getting better.

UPDATE: Dan pointed out an edge case with my plan that will definitely be a problem. I think I need to rethink whether this is even a good idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

卷耳 2024-07-17 14:23:10

是的。 您使用哪种覆盖工具?

Hudson 的 Cobertura 插件绝对支持这一点。 在项目配置屏幕上,您可以指定阈值。

或者,您可以使用 cobertura-check 任务使 Ant 构建失败(而不是 Hudson)。

编辑:我不确定您是否可以准确地完成您所要求的操作。 即使你可以,也可能会出现问题。 例如,假设您的平均覆盖率为 75%,但对于一门课程,您的覆盖率为 80%。 如果删除 80% 的类及其所有测试,就会降低总体覆盖率,即使其他代码的测试量都不比以前少。

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.

乖乖哒 2024-07-17 14:23:10

这是一种 hack,但我们出于类似的原因在 Findbugs 和 Checkstyle 中使用它。 您可以设置一个 Ant 任务来执行以下操作(可以将其拆分为多个任务,但为了简洁起见,我将它们组合在一起):

  1. 运行覆盖率测试
  2. 解析覆盖率结果并获取覆盖率百分比
  3. 读取 tmp/lastCoverage.txt来自上次构建(请参阅步骤 #5a)
  4. 将当前覆盖率百分比与从 lastCoverage.txt 读取的百分比进行比较
    1. 如果百分比没有减少,请将新的百分比写入 tmp/lastCoverage.txt 的内容
    2. 如果百分比确实减少,则保留原始文件并回显“COVERAGE FAILURE”(使用 ant 的 echo 任务)。

请注意,步骤 2 到 5 不一定需要使用本机 Ant 任务来完成 - 您可以使用 Ant 的 javac 任务来运行 Java 程序来为您执行此操作。

然后,配置Hudson:

  • 在“源代码管理”下,确保选中“使用更新”。 这将允许您的lastCoverage.txt 文件在构建之间保留。 请注意,如果您确实非常需要在构建之间清理内容,这可能会出现问题。
  • 使用 Hudson Text Finder 插件和正则表达式进行搜索构建输出中的“COVERAGE FAILURE”(确保为插件选中“Also search console output”)。 文本查找器插件可以将构建标记为不稳定。

显然,您可以将文件名/路径和控制台输出等内容替换为适合您的构建上下文的内容。

正如我上面提到的,这相当 hacky,但这可能是让 Hudson 将先前版本中的内容与当前版本进行比较的少数(唯一?)方法之一。

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.

最好是你 2024-07-17 14:23:10

另一种方法是使用 Hudson 的 Sonar 插件来保持覆盖范围随时间变化的趋势,并更容易吸收和分析结果。 它还将显示其他措施的覆盖范围,例如 checkstyle 和 pmd

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

遥远的绿洲 2024-07-17 14:23:10

Atlassian 的 Clover 支持您想要的。 查看 clover-check Ant 任务,特别是historyDir 属性。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文