eclipse 如何运行给定包的所有 JUnit 测试?

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

How to run all JUnit tests of a given package?

javaeclipsejunit

提问by snakile

I use JUnit 4 in eclipse. I have some test classes in my package and want to run them all. How?

我在 Eclipse 中使用 JUnit 4。我的包中有一些测试类并想运行它们。如何?

回答by DerMike

Right-click on the package in the package explorer and select 'Run as' and 'Unit-Test'.

右键单击包浏览器中的包,然后选择“运行方式”和“单元测试”。

回答by John

In eclipse if you right click the folder and select Run As JUnit Test only the tests in that folder will be run (i.e. tests in nested subfolders will not be run). In order to run all of the tests in a directory including tests in nested directories you will need to use something like googlecode.junittool box.

在 Eclipse 中,如果您右键单击该文件夹并选择 Run As JUnit Test,则只会运行该文件夹中的测试(即不会运行嵌套子文件夹中的测试)。为了在一个目录中运行所有测试,包括嵌套目录中的测试,您需要使用类似 googlecode.junittool 框的东西。

Using this I created something like the following

使用这个我创建了如下内容

package com.mycompany.myproject.mymodule;

import org.junit.runner.RunWith;

import com.googlecode.junittoolbox.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;

@RunWith(WildcardPatternSuite.class)
@SuiteClasses({ "**/*Test.class" })
public class RunAllMyModuleTests {
}

I added the required dependencies (jar files) using this in my mavin build (in addition to the junit dependency):

我在我的 mavin 构建中使用它添加了所需的依赖项(jar 文件)(除了 junit 依赖项):

<dependency>
    <groupId>com.googlecode.junit-toolbox</groupId>
    <artifactId>junit-toolbox</artifactId>
    <version>1.5</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit-dep</artifactId>
    <version>4.8.2</version>
</dependency>

Right clicking on this class and selecting Run As JUnit test runs all of the tests in the specified directory including all tests in nested subfolders.

右键单击该类并选择 Run As JUnit 测试运行指定目录中的所有测试,包括嵌套子文件夹中的所有测试。

回答by user85421

with JUnit 4 I like to use an annotated AllTestsclass:

对于 JUnit 4,我喜欢使用带注释的AllTests类:

@RunWith(Suite.class)
@Suite.SuiteClasses({ 

    // package1
    Class1Test.class,
    Class2test.class,
    ...

    // package2
    Class3Test.class,
    Class4test.class,
    ...

    })

public class AllTests {
    // Junit tests
}

and, to be sure that we don't forget to add a TestCase to it, I have a coverage Test (also checks if every public method is being tested).

并且,为了确保我们不会忘记向其中添加 TestCase,我有一个覆盖测试(还检查是否正在测试每个公共方法)。

回答by Gregory Pakosz

I used to declare a AllTestsclass so that I would also be able to run all tests from the command line:

我曾经声明一个AllTests类,以便我也可以从命令行运行所有测试:

public final class AllTests
{
  /**
   * Returns a <code>TestSuite</code> instance that contains all the declared
   * <code>TestCase</code> to run.
   * 
   * @return a <code>TestSuite</code> instance.
   */
  public static Test suite()
  {
    final TestSuite suite = new TestSuite("All Tests");

    suite.addTest(Test1.suite());
    suite.addTest(Test2.suite());
    suite.addTest(Test3.suite());

    return suite;
  }

  /**
   * Launches all the tests with a text mode test runner.
   * 
   * @param args ignored
   */
  public static final void main(String[] args)
  {
    junit.textui.TestRunner.run(AllTests.suite());
  }

} // AllTests

Where each test class defines

每个测试类定义的地方

  /**
   * Returns a <code>TestSuite</code> instance that contains all
   * the declared <code>TestCase</code> to run.
   * 
   * @return a <code>TestSuite</code> instance.
   */
  public static final Test suite()
  {
    return new TestSuite(Test1.class); // change the class accordingly
  }

回答by Istvan Devai

With JUnit5, you can easily create a "suite" class, that will run all tests in a package (or even subpackages, it works recursively):

使用 JUnit5,您可以轻松创建一个“套件”类,它将运行一个包(甚至子包,它以递归方式工作)中的所有测试:

@RunWith(JUnitPlatform.class)
@SelectPackages("my.test.package")
public class MySuite {
}

Once that's done, you can run this suite with "Run as Test".

完成后,您可以使用“作为测试运行”来运行此套件。

回答by tangens

In the package explorer you can use the context menu of the package and choose run as junit test.

在包浏览器中,您可以使用包的上下文菜单并选择run as junit test

回答by akuhn

Right click on the package and choose "Run as Test" from the "Run as" submenu.

右键单击包,然后从“运行方式”子菜单中选择“作为测试运行”。