Java 排除特定测试在 jUnit 中并行运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24811430/
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
Exclude specific tests from being run in parallel in jUnit
提问by BSJ
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:
我最近偶然发现了一种通过 jUnit 并行化测试执行的简单方法,方法是在 java 项目的 pom.xml 文件中指定以下内容:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>classes</parallel>
</configuration>
</plugin>
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?
我发现有 2 个测试类(我们称它们为“badtestclass1”和“badtestclass2”)由于它们中的测试的编写方式而不断受到这种并行执行的惩罚。理想情况下,我会重构这些测试类以使其表现得更好,但在此期间,我想知道是否有一种巧妙的方法可以“排除”这些特定类的并行执行。基本上,有没有办法并行执行其他所有内容,然后依次执行这两个(或其他顺序,无关紧要)。会像下面这样工作吗?
<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>
采纳答案by Patson Luk
Exclude those 2 tests in the original test phrase and then create a new execution with those 2 classes running in single thread? :)
在原始测试短语中排除这 2 个测试,然后使用在单线程中运行的这 2 个类创建新的执行?:)
<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>
回答by polaretto
You can annotate the classes you don't want parallelized with jcip @NotThreadSafeand leave the surefire configuration as it was in your starting example. This way, whenever surefire finds an annotated class it just executes it in a single thread. It's explained right herein the "Parallel Test Execution and Single Thread Execution" paragraph.
您可以使用 jcip @NotThreadSafe注释您不希望并行化的类,并保留在您的起始示例中的surefire配置。这样,只要surefire 找到一个带注释的类,它就会在单个线程中执行它。它的解释正确这里的“并行测试执行和单线程执行”段落。