【问题标题】:A better way to run lots of Integration Tests using JUnit?使用 JUnit 运行大量集成测试的更好方法?
【发布时间】:2010-01-08 11:33:25
【问题描述】:

使用 JUnit 运行大量集成测试的最佳方法是什么?

我粗略地发现下面的代码可以运行所有的测试......但它有一个巨大的缺陷。每个类中的 tearDown() 方法在它们全部运行之前不会被调用。

public class RunIntegrationTests extends TestSuite {
   public RunIntegrationTests(){
   }

   public static void main (String[] args){
      TestRunner.run(testSuite());
   }

   public static Test testSuite(){

      TestSuite result = new TestSuite();

      result.addTest(new TestSuite(AgreementIntegrationTest.class));
      result.addTest(new TestSuite(InterestedPartyIntegrationTest.class));
      result.addTest(new TestSuite(WorkIntegrationTest.class));
      // further tests omitted for readability

      return result;
   }
}

正在运行的类连接到数据库、加载对象并将其显示在 JFrame 中。我覆盖了 setVisible 方法来启用测试。在我们的构建机器上,java vm 在运行上面的代码时内存不足,因为它必须从数据库加载的对象非常大。如果在每个类完成后调用 tearDown() 方法,它将解决内存问题。

有没有更好的方法来运行它们?顺便说一句,我不得不使用 JUnit 3.8.2 - 我们仍在使用 Java 1.4 :(

【问题讨论】:

    标签: java junit integration-testing


    【解决方案1】:

    不确定这是否是问题,但根据JUnit Primer,您应该直接添加测试,而不是使用TestSuite:

      result.addTest(new AgreementIntegrationTest()));
      result.addTest(new InterestedPartyIntegrationTest()));
      result.addTest(new WorkIntegrationTest()));
    

    【讨论】:

      【解决方案2】:

      这很奇怪。 setUp 和 tearDown 应该预定每个测试方法的运行,无论这些方法如何捆绑到套件中。

      我通常会稍有不同。

      TestSuite suite = new TestSuite( "Suite for ..." ) ;
      
      suite.addTestSuite( JUnit_A.class ) ;
      suite.addTestSuite( JUnit_B.class ) ;
      

      我刚刚验证了 tearDown 确实被调用了正确的次数。但是您的方法应该也可以正常工作。

      您确定正确指定了 tearDown - 例如这不是“拆解”吗?当您单独运行一个测试类时,是否正确调用了 tearDown?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多