Java 如何让 JUnit 测试用例按顺序运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9667944/
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
How to make JUnit test cases to run in sequential order?
提问by Athiruban
I am using JUnit4.
我正在使用 JUnit4。
I have a set of test methods in a test case.
我在一个测试用例中有一组测试方法。
Each test method inserts some records and verify a test result and finally delete the records inserted.
每个测试方法插入一些记录并验证测试结果,最后删除插入的记录。
Since the JUnit run in parallel, test methods fail because of some records present during the execution of previous test method. This happen only in my colleague machine(Windows 7), not in my machine(Cent OS 6).
由于 JUnit 并行运行,测试方法会因为在先前测试方法执行期间存在的一些记录而失败。这仅发生在我的同事机器(Windows 7)中,而不发生在我的机器(Cent OS 6)中。
What we need is that the test methods have to pass in all our machines.
我们需要的是测试方法必须通过我们所有的机器。
I have tried clearing the records in the Setup() method but again it works only on my machine. Is there any option available in JUnit to make the test methods to run in a uniform sequential order ?
我曾尝试清除 Setup() 方法中的记录,但它再次仅适用于我的机器。JUnit 中是否有任何选项可以使测试方法以统一的顺序运行?
Thanks,
谢谢,
回答by Roy Truelove
Ordering of tests is not guaranteed in JUnit.
JUnit 不保证测试的顺序。
The reason for this is that unit tests are meant to be atomic - all of the setup should happen in the setup / tear down methods, but not by other tests.
这样做的原因是单元测试是原子的——所有的设置都应该在设置/拆卸方法中发生,而不是在其他测试中发生。
Consider moving the code that inserts data into another helper class that can be called by both the test that's inserting and the class that needs to verify, and calling that class in your @Before methods.
考虑将插入数据的代码移动到另一个可以被插入的测试和需要验证的类调用的辅助类中,并在 @Before 方法中调用该类。
You should also consider a mocking solution (eg Mockito) as opposed to hitting the database directly if you can - mocking will go a long way to ensuring that your tests are nice and isolated, and, as a nice side benefit, usually help point out where you could use some refactoring.
如果可以,您还应该考虑模拟解决方案(例如 Mockito),而不是直接访问数据库 - 模拟将大大确保您的测试良好和隔离,并且作为一个很好的附带好处,通常有助于指出在那里你可以使用一些重构。
回答by Matthew Farwell
Because you're running the tests in parallel, and you're hitting the database, you're highly likely to have problems, because the database won't necessarily be in a coherent state for each test.
因为您正在并行运行测试,并且您正在访问数据库,所以您很可能会遇到问题,因为对于每个测试,数据库不一定都处于一致状态。
Solution: don't run your tests in parallel. JUnit doesn't run the tests in parallel by default, so either you're setting the option in maven or using one of the parallel runners in JUnit.
解决方案:不要并行运行测试。默认情况下,JUnit 不会并行运行测试,因此您要么在 maven 中设置该选项,要么使用 JUnit 中的并行运行器之一。
If you're still having problems between tests failing on Windows but not on Cent OS, then it's maybe a problem with run order, which you'll need to fix. See my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?.
如果您在 Windows 上的测试失败之间仍然遇到问题,但在 Cent OS 上却没有,那么可能是运行顺序有问题,您需要解决这个问题。请参阅我对JUnit4 是否已开始支持测试排序的回答?是故意的吗?.
The way around this (at least in JUnit terms) is to remove the dependencies between tests. Basically, JUnit doesn't support ordering and the tests should be able to be run in any order.
解决这个问题的方法(至少在 JUnit 术语中)是删除测试之间的依赖关系。基本上,JUnit 不支持排序并且测试应该能够以任何顺序运行。
If you really need to have dependencies between tests, use TestNG, where you can have dependencies.
如果你真的需要在测试之间有依赖关系,使用 TestNG,你可以在那里有依赖关系。
回答by Aleksandr Dubinsky
JUnit 4.11 now supports specifying execution order using @FixMethodOrder
annotation.
JUnit 4.11 现在支持使用@FixMethodOrder
注解指定执行顺序。
回答by Ranga Reddy
MethodSorters is a new class introduced after Junit 4.6 release. This class declared three types of execution order, which can be used in your test cases while executing them.
MethodSorters 是 Junit 4.6 发布后引入的一个新类。这个类声明了三种类型的执行顺序,它们可以在你的测试用例执行时使用。
NAME_ASCENDING(MethodSorters.NAME_ASCENDING)- Sorts the test methods by the method name, in lexicographic order.
JVM(null)- Leaves the test methods in the order returned by the JVM. Note that the order from the JVM my vary from run to run.
DEFAULT(MethodSorter.DEFAULT)- Sorts the test methods in a deterministic, but not predictable, order.
NAME_ASCENDING(MethodSorters.NAME_ASCENDING)- 按方法名称按字典顺序对测试方法进行排序。
JVM(null)- 按照 JVM 返回的顺序保留测试方法。请注意,JVM 的顺序因运行而异。
DEFAULT(MethodSorter.DEFAULT)- 以确定性但不可预测的顺序对测试方法进行排序。
.
.
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
//Running test cases in order of method names in ascending order
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTestCasesExecution {
@Test
public void secondTest() {
System.out.println("Executing second test");
}
@Test
public void firstTest() {
System.out.println("Executing first test");
}
@Test
public void thirdTest() {
System.out.println("Executing third test");
}
}
Output:
输出:
Executing first test
Executing second test
Executing third test
Reference: http://howtodoinjava.com/2012/11/24/ordered-testcases-execution-in-junit-4/
参考:http: //howtodoinjava.com/2012/11/24/ordered-testcases-execution-in-junit-4/
回答by Rodel
There is no problem running tests in parallel even if you have your data layer in it. But you need to have additional work to create MOCK UPs for your data so not it will not hit the database. You can use different mockup frameworks like Mockito, EasyMock and Arquillian.
即使您有数据层,并行运行测试也没有问题。但是您需要进行额外的工作来为您的数据创建 MOCK UP,以免它不会访问数据库。您可以使用不同的模型框架,如 Mockito、EasyMock 和 Arquillian。