使用 JUnit 运行大量集成测试的更好方法是什么?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否是问题所在,但根据 JUnit Primer 您应该添加直接测试,而不是使用 TestSuite:
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:
这很奇怪。 setUp 和tearDown 应该结束每个测试方法的运行,无论这些方法如何捆绑到套件中。
我通常会略有不同。
我刚刚验证了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.
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?