是否有一种自动化方法来确保代码的所有部分都经过单元测试?

发布于 2024-08-27 05:40:28 字数 62 浏览 6 评论 0原文

我已经为我的类编写了 JUnit 测试,并希望它告诉我代码中是否有任何部分未经过单元测试。有办法做到这一点吗?

I have written JUnit tests for my class, and would like it to tell me if there is any part of my code that is not unit tested. Is there a way to do this?

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

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

发布评论

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

评论(5

沉睡月亮 2024-09-03 05:40:28

是的,覆盖工具如 cobertura艾玛

他们创建报告,显示源代码中的每一行以及它是否被执行(以及汇总统计数据)。

当然,他们只能向您显示代码是否运行。无法判断单元测试是否包含断言以确认结果正确。

Yes, coverage tools like cobertura or emma.

They create reports that show every line in the source code and whether it was executed or not (and aggregated statistics as well).

Of course, they can only show you if the code was run. There is no way to tell if the unit test contained assertions to confirm that the result was correct.

莳間冲淡了誓言ζ 2024-09-03 05:40:28

您需要一些代码覆盖率工具。请参阅此处 (http://java-source.net/open-source/code-coverage ) 对于某些人

如果您查看第一个,我认为它可以满足您的需求

Cobertura 是一个免费的 Java 工具,可以计算测试访问的代码的百分比。它可用于识别 Java 程序的哪些部分缺乏测试覆盖率。它基于jcoverage。 Cobertura 的特点:

  • 可以从 ant 或从
    命令行。

You need some code coverage tools. See here (http://java-source.net/open-source/code-coverage) for some

If you look at the first one I think it does what you need

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage. Features of Cobertura:

  • Can be executed from ant or from the
    command line.
二手情话 2024-09-03 05:40:28

如果您使用 Eclipse,您还可以尝试 EclEmma,它会显示您的测试覆盖了哪些源代码行。有时这比运行 Cobertura 这样的覆盖工具更有用,因为您可以从 Eclipse 内部运行单个测试,然后立即获得有关所覆盖内容的反馈。

If you use Eclipse, you can also try EclEmma, which shows you which lines of source were covered by your test. This is sometimes more useful than running a coverage tool like Cobertura because you can run a single test from inside Eclipse and then get immediate feedback on what was covered.

毁梦 2024-09-03 05:40:28

您的标题和实际问题有所不同。其他答案中提到的工具可以告诉您哪部分代码测试(=根本没有执行)。 “确保代码的所有部分都经过单元测试”是另一回事。覆盖工具可以告诉您是否所有行/指令都已执行,但它们不能保证所有内容都经过功能测试(所有数据星座、所有执行路径等)。这需要一定的脑力。
在我看来,测试覆盖率常常给人一种错误的安全感。例如,测试琐碎的吸气剂会增加很多覆盖范围,但毫无用处。

Your headline and your actual question differ. The tools mentioned in the other answers can tell you, which part of the code were not tested (=not executed at all). Making "make sure that all parts of code is unit tested" is a different thing. The coverage tools can tell you whether all lines/instructions have been executed, but they don't guarantee that everything is tested functionally (all constellations of data, all execution paths, etc.). This requires some brain power.
In my opinion, test coverage often gives a wrong feeling of safety. E.g. testing trivial getters increases coverage a lot but is rather useless.

清浅ˋ旧时光 2024-09-03 05:40:28

如果您使用的是 IntelliJ,则有一个标题为

“Run With Coverage”

的按钮Run with Coverage

If you are using IntelliJ then there is a button titled

"Run With Coverage"

Run with Coverage

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