如何同时运行spring启动测试?

n7taea2i  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(397)

我有多个spring测试,它们一个接一个地执行,而我想同时运行它们。
代码示例:

@SpringBootTest
@RunWith(Suite.class)
@Suite.SuiteClasses({
     Test1.class,
     Test2.class,
     Test3.class,
     ...
})
public class SuiteStarter { }

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
       @Autowired | @Value fields;

       @org.junit.Test
       public void test1_1() {
            Assertions.assertThat(something1());
        }
       @Test
       public void test1_2() {
            Assertions.assertThat(something2());
        }
}
       ...

有没有类似于用@async注解suite类的东西?我有数百个测试类,其中包含多个方法,因此最好的解决方案是只更改suiterunner类,并且尽可能少地进行更改,因为我害怕破坏测试。
我对所有测试都有相同的应用程序上下文,如果有帮助的话。
在套件级别并行运行junit测试?链接提供有死和答案不被接受。我不需要像本文中那样在maven build?中并行运行junit测试?。在这里,我也不需要回答与junit并行运行测试的问题,因为它有一个实验性的特性,而且代码看起来也很难看。

xienkqul

xienkqul1#

通过将此配置添加到maven,您可以使用新的Spring5特性执行并行测试:

<build>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <parallel>methods</parallel>
        <useUnlimitedThreads>true</useUnlimitedThreads>
    </configuration>
</plugin>

更多细节可以在这里找到:spring5中的并发测试执行

pbpqsu0x

pbpqsu0x2#

所以我找到的最简单、最干净的方法就是将这种依赖性添加到maven中

<!-- https://mvnrepository.com/artifact/com.mycila/mycila-junit -->
<dependency>
    <groupId>com.mycila</groupId>
    <artifactId>mycila-junit</artifactId>
    <version>1.4.ga</version>
    <scope>test</scope>
</dependency>

换个新的班级就行了

@SpringBootTest
@RunWith(ConcurrentSuiteRunner.class)
@Concurrency(value = 12)
@Suite.SuiteClasses({
...

结果
前:250秒
60秒后

相关问题