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

发布于 2024-08-17 08:23:44 字数 897 浏览 3 评论 0原文

使用 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 虚拟机会耗尽内存,因为它必须从数据库加载的对象非常大。如果在每个类完成后调用tearDown()方法,它将解决内存问题。

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

What is the best way run a lot of integration tests using JUnit?

I crudely discovered that the code below can run all the tests... but it has a massive flaw. The tearDown() method in each of those classes is not called until they have all been run.

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

The classes being run connect to the database, load an object, and display it in a JFrame. I overrode the setVisible method to enable testing. On our build machine, the java vm runs out of memory when running the code above as the objects it has to load from the database are pretty large. If the tearDown() method was called after each class finished it would solve the memory problems.

Is there a better way to run them? I'm having to use JUnit 3.8.2 by the way - we're still on Java 1.4 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-08-24 08:23:44

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

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

Not sure if this is the problem, but according to the JUnit Primer you should just add the tests directly, instead of using the TestSuite:

  result.addTest(new AgreementIntegrationTest()));
  result.addTest(new InterestedPartyIntegrationTest()));
  result.addTest(new WorkIntegrationTest()));
随梦而飞# 2024-08-24 08:23:44

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

我通常会略有不同。

TestSuite suite = new TestSuite( "Suite for ..." ) ;

suite.addTestSuite( JUnit_A.class ) ;
suite.addTestSuite( JUnit_B.class ) ;

我刚刚验证了tearDown 确实被调用了正确的次数。但你的方法应该同样有效。

您确定tearDown 已正确指定——例如,它不是“teardown”?当您单独运行一个测试类时,tearDown 是否正确调用?

That's very strange. setUp and tearDown should bookend the running of each test method, regardless of how the methods are bundled up into suites.

I typically do it slightly differently.

TestSuite suite = new TestSuite( "Suite for ..." ) ;

suite.addTestSuite( JUnit_A.class ) ;
suite.addTestSuite( JUnit_B.class ) ;

And I just verified that tearDown was indeed being called the correct number of times. But your method should work just as well.

Are you sure tearDown is properly specified -- e.g. it's not "teardown"? When you run one test class on its own, is tearDown properly called?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文