【问题标题】:How to run all JUnit tests of a given package?如何运行给定包的所有 JUnit 测试?
【发布时间】:2011-01-03 10:50:33
【问题描述】:

我在 Eclipse 中使用 JUnit 4。我的包中有一些测试类,并且想要全部运行它们。怎么样?

【问题讨论】:

    标签: java eclipse junit


    【解决方案1】:

    在包资源管理器中右键单击包并选择“运行方式”和“单元测试”。

    【讨论】:

    • 有没有简单的方法让它也包含子包?
    【解决方案2】:

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

    使用它,我创建了类似以下的内容

    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 {
    }
    

    我在我的 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>
    

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

    【讨论】:

    • 适用于 junit-toolbox 版本 @2.4 和 junit-dep @ 4.10。在我不得不在另一个包中重新打包一层的情况下非常有用
    【解决方案3】:

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

    @RunWith(Suite.class)
    @Suite.SuiteClasses({ 
    
        // package1
        Class1Test.class,
        Class2test.class,
        ...
    
        // package2
        Class3Test.class,
        Class4test.class,
        ...
    
        })
    
    public class AllTests {
        // Junit tests
    }
    

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

    【讨论】:

    • 如此简单,如此干净!
    【解决方案4】:

    我曾经声明一个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
    

    每个测试类定义的地方

      /**
       * 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
      }
    

    【讨论】:

    • 我也使用这个变种,因为它可以在夜间构建后自动运行
    • 我在网络上的许多不同地方都看到过这个建议,但对我来说似乎有一个主要缺点,即每次添加或删除一个类时,这个文件都需要更新.理想情况下,这一切都应该是自动化的,对吧?
    【解决方案5】:

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

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

    完成后,您可以使用“Run as Test”运行此套件。

    【讨论】:

      【解决方案6】:

      在包资源管理器中,您可以使用包的上下文菜单并选择run as junit test。

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-19
          相关资源
          最近更新 更多