简体   繁体   English

以随机顺序运行JUnit SuiteClasses

[英]Run JUnit SuiteClasses in a random order

I recently started grouping my JUnit tests into test suites. 我最近开始将我的JUnit测试分组到测试套件中。 So far this is working quite good for me. 到目前为止,这对我来说非常有效。 The only gripe I have with it is that the order of the @SuiteClasses annotation determines the order of test execution. 我唯一@SuiteClasses@SuiteClasses批注的顺序决定了测试执行的顺序。 I know this is the way how it's intended but I'd like to use test suites only to group tests, not to order them. 我知道这是预期的方式,但是我只想使用测试套件对测试进行分组,而不是对测试进行排序。

Since we are using these tests in an, automated, functional (Selenium) testing environment I don't want the test to always execute in the same order. 由于我们在自动化的功能(Selenium)测试环境中使用这些测试,因此我不希望测试始终以相同的顺序执行。 Does anyone know how to use test suites only for their grouping feature? 有谁知道如何仅将测试套件用于其分组功能?

Thanks in advance! 提前致谢!

I've added my code below: 我在下面添加了我的代码:

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class, Test3.class})
public class TestSuite {
private static ScreenRecorder screenRecorder;

@BeforeClass
public static void setUp() {
    screenRecorder = new ScreenRecorder(1, Data.SCREENSHOT_DIR);
    screenRecorder.startRecording(TestSuite.class.getCanonicalName());
}

@AfterClass
public static void tearDown() throws InterruptedException, CommandException {
    screenRecorder.stopRecording();
}

} }

You need to use a specific JUnit runner for all your test cases. 您需要为所有测试用例使用特定的JUnit运行器。

Here is one I wrote some time ago: 这是我前一段时间写的:

public class RandomTestRunner extends BlockJUnit4ClassRunner {

    public RandomTestRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> methods = super.computeTestMethods();
        List<FrameworkMethod> newMethods = new ArrayList<>(methods);
        Collections.shuffle(newMethods);
        return newMethods;
    }
}

You then need to add the @RunWith(RandomTestRunner.class) annotation to all the test cases classes that you want to run truly randomly, so Test1 , Test2 and Test3 in your case. 然后,您需要向要真正随机运行的所有测试用例类添加@RunWith(RandomTestRunner.class)批注,因此要在您的用例中添加Test1Test2Test3

The order of your suites will still be the same: Test1 then Test2 then Test3 , only the tests inside these classes will be random. 套件的顺序仍然相同: Test1然后Test2然后Test3 ,只有这些类中的测试是随机的。

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

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