Java 与 Junit 并行运行测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33703618/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:55:58  来源:igfitidea点击:

Running Tests in Parallel with Junit

javatestingjunit

提问by Cory Kendall

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 @BeforeClassannotations to be run once before any Test in a class runs, and I would like @AfterClassannotations 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.

我想同时@Test跨多个类运行每个用 注释的方法。在某些情况下,我想对此加以限制,并说在任何时候总共只能运行 100 个。我希望带@BeforeClass注释的方法在类中的任何测试运行之前运行一次,并且我希望@AfterClass在类中的所有测试运行后运行一次注释。我想System.outSystem.err和异常进行适当的缓冲/捕获,而不是写出来的,所以他们不交错,并且可以读取最终输出和明白发生了什么。

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.

这存在吗?我有大量独立的测试用例,我的应用程序(我相信)是线程安全的。这些测试都不依赖于 JVM,鉴于我的硬件,我希望尽快完成它们。

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 用户因为这并不容易而损失了多少时间?我可以将它构建到 Junit 中吗?在我看来,这应该像单个标志一样简单,并且“有效”。

回答by Andy Guibert

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.

您可以使用 JUnit 来完成此操作ParallelComputer(请注意,它仍被视为实验性的)。这是一个非常简单的实现,由 java.util.concurrent.ExecutorService API 支持。
如果您好奇它是如何工作的,请查看源代码

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

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

Example usage:

用法示例:

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");
        }
    }
}

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

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

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

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

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

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

回答by VdeX

Yes, You can.

是的你可以。

If you are using maven. You can take help of

如果您使用的是 Maven。你可以帮助

maven-surefire-plugin

maven-surefire-plugin

In Spring,

在春天,

You can check this Link

您可以查看此链接

<build>
    <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.7.1</version>
        <configuration>
            <parallel>classes</parallel>
            <threadCount>5</threadCount>
        </configuration>
    </plugin>
    </plugins>
</build>

Solution 2: Junit4 provides parallel feature using ParallelComputer

解决方案 2:Junit4 使用ParallelComputer提供并行特性

回答by Stefan Birkner

JUnit Toolboxprovides JUnit runners for parallel execution of tests.

JUnit Toolbox为并行执行测试提供了 JUnit 运行器。

In order to not intermix output to System.errand System.outyou have to start tests in separate JVMs, because System.errand System.outare global.

为了不混合输出到System.err和,System.out您必须在单独的 JVM 中开始测试,因为System.errSystem.out是全局的。