排除特定测试在jUnit中并行运行 [英] Exclude specific tests from being run in parallel in jUnit

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

问题描述

最近,我在Java项目的pom.xml文件中指定以下内容,偶然发现了一种通过jUnit并行执行测试的简单方法:

I recently stumbled upon a simple way to parallelize the execute of tests via jUnit by specifying the following in a java project's pom.xml file:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <configuration>
  <parallel>classes</parallel>
 </configuration>
</plugin>

我发现有2个测试类(我们称它们为"badtestclass1"和"badtestclass2")由于编写测试的方式而不断受到这种并行执行的惩罚.理想情况下,我将重构这些测试类以使其表现更好,但是在此期间,我想知道是否存在一种巧妙的方法来排除"这些特定的类以使其无法并行执行.基本上,有一种方法可以并行执行其他所有操作,然后依次执行这2个操作(或其他顺序无关紧要).像这样的事情会起作用吗?

I've discovered that there are 2 test-classes (let's call them "badtestclass1" and "badtestclass2") that keep getting penalized by this parallel execution due to the way the tests in them have been written. Ideally, I would refactor those test-classes to behave better, but in the interim, I was wondering if there's a nifty way to "exclude" these specific classes from being executed in parallel. Basically, is there a way to execute everything else in parallel and then these 2 in sequence (or the other order, doesn't matter). Would something like the following work?

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <configuration>
  <parallel>classes</parallel>
  <excludes>
   <excludesFile>badtestclass1</excludesFile>
   <excludesFile>badtestclass2</excludesFile>
  </excludes>
 </configuration>
</plugin>

推荐答案

在原始测试短语中排除这2个测试,然后使用在单线程中运行的这2个类创建一个新的执行? :)

Exclude those 2 tests in the original test phrase and then create a new execution with those 2 classes running in single thread? :)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>path/to/your/class/badtestclass1.java</exclude>
            <exclude>path/to/your/class/badtestclass2.java</exclude>
        </excludes>
        <parallel>classes</parallel>
    </configuration>

    <executions>
        <execution>
            <id>single-thread-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>path/to/your/class/badtestclass1.java</include>
                    <include>path/to/your/class/badtestclass2.java</include>
                </includes>
                <threadCount>1</threadCount>
            </configuration>
        </execution>
    </executions>
  </plugin>

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

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