防止junit测试运行两次 [英] Prevent junit tests from running twice

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

问题描述

我的问题有很多类似的问题,但是没有明确的答案! 我的测试失败了,因为它们在套件内运行一次,并且一次运行一次.而且我需要它们在套件中只能运行一次. 这是我的套房:

There are many similar questions to my questions,but there is no clear answer for it! My tests are failing because they are running once inside suite and once alone. And I need them to run only once inside suite. This is my suite:

@RunWith(Suite.class)
@Suite.SuiteClasses({Test1.class, Test2.class})
{
.....
}

我正在使用命令test从命令行运行测试.

I am running the test from the command line with command test.

有人找到了解决方案吗?

Has anyone found a solution for this?

推荐答案

我使用以下设置与JUnit并行运行测试,并且它们仅运行一次:

I use the following setup to run tests with JUnit, parallel, and they run only once:

@RunWith(ParallelSuite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class AllTests {

}

我有一个ParallelSuite.class:

And I have a ParallelSuite.class:

package tests;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.internal.runners.*;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.junit.runners.model.RunnerScheduler;

public class ParallelSuite extends Suite {
    public ParallelSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError  {

        super(klass, builder);

        setScheduler(new RunnerScheduler() {

            private final ExecutorService service = Executors.newFixedThreadPool(4);

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

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

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