简体   繁体   English

如何使JUnit测试用例按顺序运行?

[英]How to make JUnit test cases to run in sequential order?

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. 由于JUnit并行运行,因为在执行上一个测试方法期间存在一些记录,测试方法会失败。 This happen only in my colleague machine(Windows 7), not in my machine(Cent OS 6). 这只发生在我的同事机器(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. 我已经尝试清除Setup()方法中的记录,但它再次只能在我的机器上运行。 Is there any option available in JUnit to make the test methods to run in a uniform sequential order ? JUnit中是否有任何选项可以使测试方法以统一的顺序运行?

Thanks, 谢谢,

JUnit 4.11现在支持使用@FixMethodOrder注释指定执行顺序。

MethodSorters is a new class introduced after Junit 4.6 release. MethodSorters是Junit 4.6发布后引入的新类。 This class declared three types of execution order, which can be used in your test cases while executing them. 该类声明了三种类型的执行顺序,可以在执行它们时在测试用例中使用。

  1. NAME_ASCENDING(MethodSorters.NAME_ASCENDING) - Sorts the test methods by the method name, in lexicographic order. NAME_ASCENDING(MethodSorters.NAME_ASCENDING) - 按字典顺序按方法名称对测试方法进行排序。

  2. JVM(null) - Leaves the test methods in the order returned by the JVM. JVM(null) - 按JVM返回的顺序保留测试方法。 Note that the order from the JVM my vary from run to run. 请注意,来自JVM的顺序因运行而异。

  3. DEFAULT(MethodSorter.DEFAULT) - Sorts the test methods in a deterministic, but not predictable, order. 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/

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),而不是直接命中数据库,如果可以的话 - 模拟将大大有助于确保您的测试很好并且孤立,并且,作为一个很好的附带好处,通常有助于指出你可以在哪里使用一些重构。

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. 如果您在Windows上测试失败但在Cent OS上没有出现问题,则可能是运行顺序问题,您需要修复它。 See my answer to Has JUnit4 begun supporting ordering of test? 看看我的答案是否有JUnit4开始支持测试订购? Is it intentional? 这是故意的吗? .

The way around this (at least in JUnit terms) is to remove the dependencies between tests. 解决这个问题的方法(至少在JUnit术语中)是删除测试之间的依赖关系。 Basically, JUnit doesn't support ordering and the tests should be able to be run in any order. 基本上,JUnit不支持排序,测试应该能够以任何顺序运行。

If you really need to have dependencies between tests, use TestNG, where you can have dependencies. 如果您确实需要在测试之间存在依赖关系,请使用TestNG,您可以在其中拥有依赖项。

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. 但是你需要额外的工作来为你的数据创建MOCK UP,这样就不会碰到数据库了。 You can use different mockup frameworks like Mockito, EasyMock and Arquillian. 您可以使用不同的模型框架,如Mockito,EasyMock和Arquillian。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM