【问题标题】:Gradle is executing tests terribly slow because it is adding to much tests to the suiteGradle 执行测试非常慢,因为它向套件中添加了许多测试
【发布时间】:2017-05-07 03:58:45
【问题描述】:

我有大约 8 个end-to-end-test扩展 我的 abstract SpringContextLoadingTest 类,如下所示:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public abstract class SpringContextLoadingTest extends AbstractTestNGSpringContextTests {
}

我有带有 @SpringBootApplication 注释的主应用程序类。

当我使用 TestNG 时,我在一个组中有一些类(“通道 A”),而在另一个组中有一些类(“通道 B”)。

我为运行不同的组制作了 gradle 任务:

task runChannelA(type: Test) {
    forkEvery = 1
    useTestNG() {
        includeGroups "channel A"
    }
}

如果没有“forEvery = 1”,当运行超过 1 个测试时,端口繁忙会出现问题。

感谢下面这个简单的配置,我从 gradle 任务执行中收到了更详细的输出:

tasks.withType(Test) {
    testLogging.showStandardStreams = true
}

没有它,在执行测试后,应用程序会在关闭 EntityManagerFactory 时挂起 2 分钟,但这个标志表明 gradle 拾取了它没有被要求的测试。对于每个测试,无论它在哪个组中,gradle 都会记录:

Gradle Test Executor 22 STANDARD_OUT
2016-12-21 17:10:00.115  INFO   --- [    Test worker] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [mypackage.OtherTest], using SpringBootContextLoader
2016-12-21 17:10:00.141  INFO   --- [    Test worker] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [mypackage.OtherTest]: no resource found for suffixes {-context.xml, Context.groovy}.
2016-12-21 17:10:00.143  INFO   --- [    Test worker] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [mypackage.OtherTest]: DbCongestionExploratoryTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2016-12-21 17:10:00.455  INFO   --- [    Test worker] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration mypackage.Application for test class mypackage.OtherTest
2016-12-21 17:10:00.466  INFO   --- [    Test worker] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@9404cc4, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@46876feb, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@dd46df5, org.springframework.test.context.support.DirtiesContextTestExecutionListener@49e2c374]

这需要很多时间,因为我还有很多其他测试。这发生在我在 IntelliJ 中看到我想要执行的测试已经通过之后。例如,我在 25 秒后看到测试已通过,但因为它正在执行与我的项目中以这种方式设置的所有其他测试一样的地狱,runChannelA 需要 3 分钟以上。有趣的是,我可以在这种奇怪的行为中停止该过程,而 IntelliJ 中的进度条会填满到最后,就像什么都没发生一样,一切都是绿色和美好的。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: java intellij-idea gradle spring-boot testng


    【解决方案1】:

    感谢对这个问题的回答,我已经解决了我的问题- Reuse spring application context across junit test classes.

    "因为 Spring 不知道 JUnit 何时完成,它会永久缓存所有上下文并使用 JVM 关闭挂钩关闭它们。"

    这就是为什么对于不同的上下文需要不同的 jvm,因为只有在关闭 jvm 时才会删除上下文。

    不幸的是,如果您在 gradle 任务中使用 'forkEvery =1',那么 Spring 将为您在类路径中的 每个 上下文创建 jvm,包括在您的任何测试中未使用的上下文当前正在执行。

    我有许多不同的上下文,因为我试图为每个测试只加载最低要求的配置。

    我已尽可能解决了按上下文对测试进行分组的问题。我已扩展 SpringContextLoadingTest 以加载更多内容。它现在涵盖了我的大部分测试。使用此上下文的测试在一个 gradle 任务中运行,没有 forkEvery = 1。我需要加载不同上下文的测试,(因为我更改了一些属性或加载了不同版本的 bean)我运行另一个 gradle 任务,这次指定了 forkEvery = 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      相关资源
      最近更新 更多