【问题标题】:Gradle run test classes in parallel with methods of same class running in same threadGradle 与在同一线程中运行的同一类的方法并行运行测试类
【发布时间】:2017-11-28 13:15:37
【问题描述】:

我有几个 Junit 测试课程。我想在 2 个线程中运行它们,所以包括 maxParallelForks = 2 。我想确保同一类的测试按顺序在同一线程中运行。如何做到这一点? (我使用 SpringRunner。)

【问题讨论】:

    标签: java gradle junit springrunner


    【解决方案1】:

    我使用@RunWith(Suite.class) 运行多个测试类。所以我创建了一个新的 Runner 类,这解决了我的问题。

    public class ParallelExecutor extends Suite {
    
        public ParallelExecutor(Class<?> klass, RunnerBuilder builder) throws InitializationError, IOException, InterruptedException {
            super(klass, builder);
    
            setScheduler(new RunnerScheduler() {
    
                private final ExecutorService service = Executors.newFixedThreadPool(10);
    
                public void schedule(Runnable childStatement) {
                    service.submit(childStatement);
                }
    
                public void finished() {
                    try {
                        service.shutdown();
                        service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace(System.err);
                    }
                }
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      从快速浏览 Gradle 源代码中可以看出,这应该正是您想要的选项。 maxParallelForks 使测试并行执行,而不是单个测试方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多