简体   繁体   English

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

[英]How to run all JUnit tests of a given package?

I use JUnit 4 in eclipse. 我在eclipse中使用JUnit 4。 I have some test classes in my package and want to run them all. 我的包中有一些测试类,想要运行它们。 How? 怎么样?

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

In eclipse if you right click the folder and select Run As JUnit Test only the tests in that folder will be run (ie tests in nested subfolders will not be run). 在eclipse中,如果右键单击该文件夹并选择Run As JUnit Test,则只运行该文件夹中的测试(即不会运行嵌套子文件夹中的测试)。 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. 为了在包含嵌套目录中的测试的目录中运行所有测试,您需要使用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 test将运行指定目录中的所有测试,包括嵌套子文件夹中的所有测试。

I used to declare a AllTests class 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
  }

with JUnit 4 I like to use an annotated AllTests class: 使用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,我有一个覆盖测试(还检查是否正在测试每个公共方法)。

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

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". 完成后,您可以使用“Run as Test”运行此套件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM