与Junit并行运行测试 [英] Running Tests in Parallel with Junit

查看:80
本文介绍了与Junit并行运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时在多个类上运行每个带有 @Test 注释的方法.在某些情况下,我想限制此范围,并说一次只能运行100个总数.我希望带有 @BeforeClass 批注的方法在类中的任何测试运行之前运行一次,并且我希望 @AfterClass 批注在所有测试中运行一次.上课.我希望对 System.out System.err 和Exceptions进行适当的缓冲/捕获而不是写出,以使它们不会交错,并且我可以阅读最终输出并了解发生了什么.

I would like to run every method annotated with @Test, across multiple classes, at the same time. For some cases, I would like to limit this, and say that only 100 total can run at any one time. I would like methods with @BeforeClass annotations to be run once before any Test in a class runs, and I would like @AfterClass annotations to be run once after all Tests in a class run. I would like System.out, System.err, and Exceptions to be appropriately buffered/captured rather than written out, so that they don't interleave, and I can read the final output and understand what happened.

这存在吗?我有大量的独立测试用例,我的应用程序是(我相信)线程安全的.这些测试都没有JVM以外的依赖项,考虑到我的硬件,我想尽快完成它们.

Does this exist? I have a large number of independent test cases, and my application is (I believe) threadsafe. None of these tests have dependencies out of the JVM, and I want to finish them as quickly as possible, given my hardware.

如果不存在,是否有具体原因?全球范围内的junit用户损失了多少时间,因为这并不容易?我可以将其内置到Junit中吗?在我看来,这应该像一个标志一样简单,并且行之有效".

If this doesn't exist, is there a concrete reason why not? How much time is lost by junit users worldwide because this isn't easy? Can I build it into Junit? In my mind, this should be as simple as a single flag, and it "just works".

推荐答案

您可以使用JUnit的 ParallelComputer 来完成此操作(请注意,它仍被认为是实验性的).这是一个非常简单的实现,由java.util.concurrent.ExecutorService API支持.
如果您想知道它的工作方式,请

You can accomplish this with JUnit's ParallelComputer (note it's still considered experimental). It's a pretty simple implementation which is backed by the java.util.concurrent.ExecutorService API.
If you're curious how it works, check out the source.

基本上,您调用 JUnitCore.runClasses(Computer,Classes ...)并将第一个参数传入 ParallelComputer 对象.

Basically you call JUnitCore.runClasses(Computer, Classes ...) and pass in a ParallelComputer object for the first argument.

示例用法:

import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.runner.JUnitCore;

public class ParallelComputerExample {

    @Test
    public void runAllTests() {
        Class<?>[] classes = { ParallelTest1.class, ParallelTest2.class };

        // ParallelComputer(true,true) will run all classes and methods 
        // in parallel.  (First arg for classes, second arg for methods)
        JUnitCore.runClasses(new ParallelComputer(true, true), classes);
    }

    public static class ParallelTest1 {
        @Test
        public void test1a() {
            lookBusy(3000);
        }

        @Test
        public void test1b() {
            lookBusy(3000);
        }
    }

    public static class ParallelTest2 {
        @Test
        public void test2a() {
            lookBusy(3000);
        }

        @Test
        public void test2b() {
            lookBusy(3000);
        }
    }

    public static void lookBusy(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }
    }
}

上面的代码将在3秒钟内运行,因为所有方法和类都是并行运行的.

The above code will run in 3 seconds because all methods and classes are ran in parallel.

这将在6s内运行(因为所有类都是并行的). JUnitCore.runClasses(new ParallelComputer(true,false),classes);

This will run in 6s (because all classes are in parallel). JUnitCore.runClasses(new ParallelComputer(true, false), classes);

这也将在6s内运行(因为所有方法都是并行的). JUnitCore.runClasses(new ParallelComputer(false,true),classes);

This will also run in 6s (because all methods are in parallel). JUnitCore.runClasses(new ParallelComputer(false, true), classes);

这篇关于与Junit并行运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆