自动生成 Java 的单元测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2131935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:55:12  来源:igfitidea点击:

Automatic generation of unit tests for Java?

javaunit-testing

提问by b.roth

Any tool for automatically generating unit tests for Javacode?

有没有自动为Java代码生成单元测试的工具?

UPDATE:Primary intended use is to quickly generate unit tests for legacy code that is going to be refactored. No need to automatically keep the tests in sync with the code after automatic generation.

更新:主要用途是为将要重构的遗留代码快速生成单元测试。自动生成后无需自动保持测试与代码同步。



Almost same question was asked here, but the answer was for .NET instead of Java (Pexwas the accepted answer):

这里问了几乎相同的问题,但答案是针对 .NET 而不是 Java(Pex是公认的答案):

Automatic generation of Unit test cases for .NET and Java

自动生成 .NET 和 Java 的单元测试用例

采纳答案by Paul Wagland

All three products have a try before you buy option, so you could use them to compare the generated code against a small subset of your application.

所有这三种产品都有“先试后买”选项,因此您可以使用它们将生成的代码与应用程序的一小部分进行比较。

回答by Dror Helper

I think Agitar had a tool like this called AgitarOne. I think you can still get it but make sure the company is still there.

我认为 Agitar 有一个这样的工具,叫做AgitarOne。我认为你仍然可以得到它,但要确保公司还在那里。

回答by Karussell

NetBeans does it automatically from Java source files (I guess the other IDEs too).

NetBeans 从 Java 源文件中自动执行此操作(我猜其他 IDE 也是如此)。

Maybe you could take a look into the junit plugin (link is currently down :-()?

也许您可以查看 junit 插件(链接目前已关闭 :-()?

回答by eis

Commercial tools:

商业工具:

  • AgitarOneseems to be the biggest player in the field. There exists also 30-day trialand free web interface called JUnit Factory. There's an independent review of the tool usage in here, for example. Test generation of AgitarOne heavily focuses on the regression tests.
  • Jtestis another big player, which has been around since 1998. A free trial exists. Both AgitarOne and Jtest have been awardedmultiple times, and they are both Eclipse-based IDE:s.
  • SilverMark Test Mentor, Java Editionis a commercial product that apparently can also create tests based on java code. This product I have no experience with.
  • AgitarOne似乎是该领域最大的参与者。还有30 天试用版和称为JUnit Factory 的免费 Web 界面。例如,此处对工具使用情况进行了独立。AgitarOne 的测试生成主要集中在回归测试上。
  • Jtest是另一个大玩家,它自 1998 年以来一直存在。存在免费试用版。无论AgitarOne和Jtest的已授予多次,并且它们都是基于Eclipse的IDE:秒。
  • SilverMark Test Mentor,Java 版是一个商业产品,显然也可以创建基于 Java 代码的测试。这个产品我没有经验。

Free tools:

免费工具:

  • CodePlex AnalytiXused to be a commercial tool (it was the first commercial plugin for Eclipse back in 2001), but since Google bought the company developing it, the product has been released as free. It's a full IDE based on Eclipse and a finalist of Jolt Award in test generation at 2007. The features are nice, but the tool hasn't been updated in long time, so it unfortunately doesn't work with current versions of eclipse, and seems to have problems with code requiring java >= 1.5.
  • EvoSuiteis a a winner of SBST (Search-Based Software Testing) '13 unit test generation competitionas well as SBST '17 unit test generation competition. There exists an Eclipse plugin, web interface and command line version of the tool. There is a regression criteria documented, but apparently it doesn't work yet - the tests generated target for branch coverage, weak mutation coverage or strong mutation coverage.
  • Randoopuses feedback-directed random test generation approach. The tool has been around since 2007 and has been in continuous development. Randoop can produce both regression tests and error-finding tests. It's a robust and reliable command line tool, and Eclipse plugin exists as well.
  • Palusclaims to use both dynamic and static analysis, and is building on top of Randoop. Its authors claim that it has been used internally at Google. My experience with this software is that at least the current version (0.2) seems to have some internal issues and it doesn't seem to scale up well.
  • Daikon, Eclat, Javalanche, Java PathFinder, JCrasher, jWalk, Korat, RecGenand ReCrashare free tools that are available as well.
  • CodePlex AnalytiX曾经是一个商业工具(早在 2001 年它是 Eclipse 的第一个商业插件),但自从 Google 收购了开发它的公司后,该产品就免费发布了。它是一个基于 Eclipse 的完整 IDE,并且是 2007 年测试生成中 Jolt 奖的决赛入围者。功能很好,但是该工具已经很长时间没有更新了,因此很遗憾它不能与当前版本的 Eclipse 一起使用,并且似乎需要 java >= 1.5 的代码有问题。
  • EvoSuiteSBST(基于搜索的软件测试)'13 单元测试生成竞赛以及SBST '17 单元测试生成竞赛的获胜者。存在该工具的 Eclipse 插件、Web 界面和命令行版本。有记录的回归标准,但显然它还不起作用 - 测试生成了分支覆盖、弱突变覆盖或强突变覆盖的目标。
  • Randoop使用反馈导向的随机测试生成方法。该工具自 2007 年以来一直存在,并一直在不断发展。Randoop 可以生成回归测试和错误发现测试。它是一个强大而可靠的命令行工具,并且还存在 Eclipse 插件。
  • Palus声称同时使用动态和静态分析,并且构建在 Randoop 之上。它的作者声称它已在谷歌内部使用。我对这个软件的经验是,至少当前版本 (0.2) 似乎存在一些内部问题,而且似乎无法很好地扩展。
  • DaikonEclatJavalancheJava PathFinderJCrasherjWalkKoratRecGenReCrash也是可用的免费工具。

Note that tests generated by Jtest and AgitarOne both depend on licenced runners and they can't be run if you don't have their plugins integrated. This is not the case for free tools I've tested, which generate independent tests.

请注意,由 Jtest 和 AgitarOne 生成的测试都依赖于获得许可的运行器,如果您没有集成它们的插件,它们将无法运行。我测试过的免费工具不是这种情况,它会生成独立的测试。

Disclaimer: I'm not affiliated with any of these tools but I have done academic research on the subject.

免责声明:我不隶属于这些工具中的任何一个,但我对该主题进行了学术研究。

回答by james.garriss

Netbeans 8 has a built-in capability to build unit tests that are more than stubs. To use this feature:

Netbeans 8 具有构建比存根更多的单元测试的内置功能。要使用此功能:

  1. Open the class you want to test.
  2. Select Create/Update Testsfrom the Toolsmenu.
  3. Select whatever features you want.
  4. Open the created unit test and finish the remaining parts of the test.
  1. 打开您要测试的课程。
  2. 选择Create/Update TestsTools菜单。
  3. 选择您想要的任何功能。
  4. 打开创建的单元测试并完成测试的其余部分。

回答by user2579911

How about using Randoop https://randoop.github.io/randoop/. This works for both Java and .NET

如何使用 Randoop https://randoop.github.io/randoop/。这适用于 Java 和 .NET