【问题标题】:Running Tests in Parallel with Junit [duplicate]与 Junit 并行运行测试 [重复]
【发布时间】:2016-02-15 16:14:20
【问题描述】:

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

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

如果这不存在,是否有具体的理由不存在?全世界的junit用户因为这并不容易而浪费了多少时间?我可以将它构建到 Junit 中吗?在我看来,这应该像单个标志一样简单,并且“可以正常工作”。

【问题讨论】:

    标签: java testing junit


    【解决方案1】:

    您可以使用 JUnit 的 ParallelComputer 来完成此操作(请注意,它仍被认为是实验性的)。这是一个非常简单的实现,由 java.util.concurrent.ExecutorService API 提供支持。
    如果您对它的工作原理感到好奇,check out the source

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

    示例用法:

    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 秒内运行,因为所有方法和类都是并行运行的。

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

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

    【讨论】:

    【解决方案2】:

    JUnit Toolbox 提供用于并行执行测试的 JUnit 运行器。

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

    【讨论】:

    【解决方案3】:

    是的,你可以。

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

    ma​​ven-surefire-plugin

    春天,

    你可以查看这个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>
    

    解决方案 2:Junit4 使用 ParallelComputer

    提供并行功能

    【讨论】:

    • 如何确保测试并行运行?我没有看到测试用例的执行时间有任何减少
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多