Java 是否可以在 Eclipse 中从多个包运行 JUnit 测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/413214/
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-11 14:20:13  来源:igfitidea点击:

Is it possible to run JUnit tests from multiple packages in Eclipse?

javaeclipsetestingjunit

提问by will

Is it possible to run JUnit tests for multiple packages at the same time without manually creating test suites.

是否可以同时为多个包运行 JUnit 测试,而无需手动创建测试套件。

For example if I have the hierarchy:

例如,如果我有层次结构:

code.branchone
code.branchone.aaa
code.branchone.bbb
code.branchtwo
code.branchtwo.aaa
code.branchtwo.bbb

code.branchone
code.branchone.aaa
code.branchone.bbb
code.branchtwo
code.branchtwo.aaa
code.branchtwo.bbb

Is it possible to:

是否有可能:

  1. Run all tests in code.branchone and in descendent packages
  2. Run all tests in say code.branchone.aaa and code.branchtwo.bbb
  1. 在 code.branchone 和后代包中运行所有测试
  2. 在说 code.branchone.aaa 和 code.branchtwo.bbb 中运行所有测试

The problem I see with manually creating test suites is that when new tests come along you may forget to add them.

我看到手动创建测试套件的问题是,当新测试出现时,您可能会忘记添加它们。

回答by chillysapien

I beleieve that you can add all your test packages to a single directory. If you right click on this directory, then you should find the "run as -> JUnit test" option available. This will run all tests contained in the directory and will catch anything you've added. Any new tests get put in there with the rest of them and whatever package name you have it doesn't matter. Hope that helps

我相信您可以将所有测试包添加到一个目录中。如果您右键单击该目录,那么您应该会找到可用的“run as -> JUnit test”选项。这将运行目录中包含的所有测试,并将捕获您添加的任何内容。任何新测试都会与其余测试一起放在那里,无论您拥有什么包名称都无关紧要。希望有帮助

回答by Phil

Sure, right-click on the packages you want, and select Run As... JUnit Test

当然,右键单击您想要的包,然后选择 Run As... JUnit Test

回答by bruno conde

In Eclipse, on your debug/run configurations you have the following options:

在 Eclipse 中,在您的调试/运行配置中,您有以下选项:

  1. Run a single test
  2. Run all tests in the selected project, package or source folder
  1. 运行单个测试
  2. 运行所选项目、包或源文件夹中的所有测试

I think the second option is your friend in this case.

在这种情况下,我认为第二个选择是你的朋友。

回答by Jurgen Hannaert

I'm sure u can tweak this a bit. Make a Collection of the CLASSES_DIR property and loop over it in the findClasses method. (junit4)

我相信你可以稍微调整一下。创建 CLASSES_DIR 属性的集合并在 findClasses 方法中循环遍历它。(junit4)

http://burtbeckwith.com/blog/?p=52

http://burtbeckwith.com/blog/?p=52

回答by jhegedus

An other way:

其它的办法:

Click on the black triangle denoted by red rectangle in the picture below (in your Eclipse, not here :).)

单击下图中红色矩形表示的黑色三角形(在您的 Eclipse 中,而不是这里 :)。

enter image description here

在此处输入图片说明

Then open run configurations, create a new configuration and then set "Run all tests..." as exemplified in the image below.

然后打开运行配置,创建一个新配置,然后设置“运行所有测试...”,如下图所示。

enter image description here

在此处输入图片说明

回答by span

Yes, it is possible. The easiest way for me at least is to add a test suite class. It can look like this:

对的,这是可能的。至少对我来说最简单的方法是添加一个测试套件类。它看起来像这样:

package tests;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import tests.message.ATest;
import tests.validator.BTest;
import tests.validator.CTest;
import tests.validator.DTest;

@RunWith(Suite.class)
@SuiteClasses({ ATest.class, 
        BTest.class, 
        CTest.class, 
        DTest.class })
public class AllTests {

}

This will allow you to test any class that you import no matter what package it is in. To run this in eclipse you just right click the AllTests class and run it as JUnit test. It will then run all the tests you define in @SuiteClasses.

这将允许您测试您导入的任何类,无论它在哪个包中。要在 eclipse 中运行它,您只需右键单击 AllTests 类并将其作为 JUnit 测试运行。然后它将运行您在 中定义的所有测试@SuiteClasses

This will work with linked sources as well, I use it all the time.

这也适用于链接的源,我一直在使用它。

回答by icyerasor

Maybe not exactly what the original question was, but you can easily run all tests of a whole Project, by simply right-clicking the project -> Run As JUnitTest. Don't worry where the annotated classes reside, this will be scanned.

也许不完全是最初的问题,但是您可以轻松地运行整个 Project 的所有测试,只需右键单击该项目 -> Run As JUnitTest。不要担心带注释的类位于何处,这将被扫描。

This does not work if applied to the test-src-folder or a package with subpackes. Quite a shame actually -.-

如果应用于 test-src-folder 或带有子包的包,这将不起作用。实际上很遗憾-.-