@QuarkusTest 单元测试需要很长时间

@QuarkusTest unit tests take a long time

我开始了一个项目,现在我的项目中有大约 7 个测试,使用 gradle test.

执行整个测试 suite 已经花费了一分多钟

从附加输出(--info 标志)我可以看到整个 quarkus 应用程序以及 mongodb 实例等依赖项在每个测试 class 和方法中都重新启动。

这与 quarkus 文档在测试 guide 页面上所说的完全相反:

So far in all our examples we only start Quarkus once for all tests. Before the first test is run Quarkus will boot, then all tests will run, then Quarkus will shutdown at the end. This makes for a very fast testing experience however it is a bit limited as you can’t test different configurations.

所有测试都用 @QuarkusTest 注释,每个测试只测试一个端点。

我使用“纯”kotlin (1.5.21)、Quarkus 2.2 版。2.Final 和 gradle 6.9。 安装的功能:cdi、config-yaml、jacoco、kotlin、mongodb-client、mongodb-panache-kotlin、narayana-jta、rest-client、rest-client-jackson、resteasy、resteasy-jackson , smallrye-context-propagation, smallrye-health, smallrye-openapi, swagger-ui

这是正常行为吗?如果是,具有数百个测试的应用程序很容易需要 ~20 分钟或更长时间才能 运行 整个测试 suite。

我还没有尝试过 maven,所以我无法验证这不是一个 gradle 相关的问题。

在尝试用新项目重现它时,我想我发现了我的代码的问题: 我还在测试中使用了 @QuarkusTestResourcesrestrictToAnnotatedClass=true。 这意味着必须重新加载配置和测试配置文件,因此也必须重新加载 quarkus 应用程序。 显然,所有 DevServices 也都重新启动了(在我的例子中是 mongodb,因为我使用的是 panache 扩展),这解释了测试的长时间运行。

我稍微重新组织了我的测试,因此它们使用“全局”测试资源(在我的例子中是 WireMockServer)。 现在 quarkus 只在测试前启动一次,gradle 测试任务的总运行时间是可以接受的。