Eclipse 中的 JUnit 测试在一起运行时失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4286533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit tests in eclipse failing when run together
提问by pyvi
I have a set of 44 JUnit tests that I run using Eclipse (I got those tests from someone else - I am new to JUnit tests). When I run them all together, 24 of them fail. However, if I then run the failing ones individually, some of them do pass after all. The tests do take a little time - one of the failing ones for example takes about one or two minutes to complete, while just letting all of them run finishes in just a few seconds.
我有一组 44 个使用 Eclipse 运行的 JUnit 测试(我从其他人那里得到了这些测试 - 我是 JUnit 测试的新手)。当我一起运行它们时,其中 24 个失败了。但是,如果我然后单独运行失败的那些,毕竟其中一些确实通过了。测试确实需要一点时间 - 例如,其中一个失败的测试需要大约一两分钟才能完成,而让所有测试在短短几秒钟内完成。
I am starting multiple tests by right-clicking on the folder they are in and selecting "Run As -> JUnit Test". I am using JUnit 3. Am I doing something wrong in starting them / is there some kind of option I am missing?
我通过右键单击它们所在的文件夹并选择“运行方式 - > JUnit 测试”来启动多个测试。我正在使用 JUnit 3。我在启动它们时做错了什么/是否缺少某种选项?
回答by GaryF
It's hard to say for sure without seeing the tests, but it sounds to me like they share some state or resource that is not being reset correctly for the next test.
没有看到测试就很难确定,但在我看来,它们共享某些状态或资源,但在下一次测试中没有正确重置。
回答by Steve Emmerson
GaryF's answer is one possibility. Another is that the tests have a race-condition: whether or not a test succeeds depends on how fast something occurs (which can vary due to the vagaries of the O/S). If you run the failing tests separately, do they alwayssucceed or do they sometimes fail. If they sometimes fail, then you likely have a race-condition.
GaryF 的回答是一种可能性。另一个是测试具有竞争条件:测试是否成功取决于某事发生的速度(这可能因 O/S 的变幻莫测而有所不同)。如果单独运行失败的测试,它们是总是成功还是有时会失败。如果它们有时会失败,那么您可能存在竞争条件。
回答by Riggy
To expand on Gary's answer, when right-clicking and doing the Run As -> JUnit, you can't guarantee the order in which the tests are run, which could also help to corrupt a shared resource.
为了扩展 Gary 的答案,当右键单击并执行 Run As -> JUnit 时,您无法保证测试的运行顺序,这也可能有助于破坏共享资源。
I would start by looking at your setup() and teardown() methods to ensure that shared resources are being reset properly. Also, since you inherited these tests, you may want to look at whether any of the tests are dependent upon one another. While this is a bad practice and should be changed, you could perhaps create a test suite() to ensure the order in which they're run (at least until you can re-factor and decouple the tests).
我将首先查看您的 setup() 和 teardown() 方法,以确保正确重置共享资源。此外,由于您继承了这些测试,您可能需要查看是否有任何测试相互依赖。虽然这是一种不好的做法并且应该改变,但您或许可以创建一个测试套件()来确保它们的运行顺序(至少在您可以重构和解耦测试之前)。