Continuous Testing

Prerequisites

include::./_includes/prerequisites.adoc[]* Getting Started Guide的完成问候应用程序

Introduction

Quarkus 支持持续测试,其中测试会在代码更改保存后立即运行。这使您可以对代码更改获得即时反馈。Quarkus 检测哪些测试覆盖哪些代码,并使用此信息仅在代码更改时运行相关测试。

Solution

使用以下命令启动 Getting Started应用程序(或任何其他应用程序):

CLI
quarkus dev
Maven
./mvnw quarkus:dev
Gradle
./gradlew --console=plain quarkusDev

Quarkus 会像往常一样以开发模式启动,但在屏幕底部,您应该看到以下内容:

--
Tests paused, press [r] to resume, [h] for more options>

按 `r`测试将开始运行。您应该看到状态在运行时在屏幕底部发生更改,并且它应该以以下内容结束:

--
Tests all passed, 2 tests were run, 0 were skipped. Tests took 1470ms.
Press [r] to re-run, [v] to view full results, [p] to pause, [h] for more options>

如果您希望持续测试自动启动,则可以在 application.properties`中设置 `quarkus.test.continuous-testing=enabled。如果您根本不需要它,则可以将其更改为 disabled

现在,您可以开始对应用程序进行更改。进入 GreetingResource`并将 hello 端点更改为 `"hello world",然后保存文件。Quarkus 应立即重新运行测试,您应该获得类似于以下内容的输出:

2021-05-11 14:21:34,338 ERROR [io.qua.test] (Test runner thread) Test GreetingResourceTest#testHelloEndpoint() failed
: java.lang.AssertionError: 1 expectation failed.
Response body doesn't match expectation.
Expected: is "hello"
  Actual: hello world

	at io.restassured.internal.ValidatableResponseImpl.body(ValidatableResponseImpl.groovy)
	at org.acme.getting.started.GreetingResourceTest.testHelloEndpoint(GreetingResourceTest.java:21)


--
Test run failed, 2 tests were run, 1 failed, 0 were skipped. Tests took 295ms
Press [r] to re-run, [v] to view full results, [p] to pause, [h] for more options>

将其改回来,测试将再次运行。

Controlling Continuous Testing

可以使用各种热键来控制持续测试。按 `h`将显示以下命令列表:

The following commands are available:
[r] - Re-run all tests
[f] - Re-run failed tests
[b] - Toggle 'broken only' mode, where only failing tests are run (disabled)
[v] - Print failures from the last test run
[p] - Pause tests
[o] - Toggle test output (disabled)
[i] - Toggle instrumentation based reload (disabled)
[l] - Toggle live reload (enabled)
[s] - Force restart
[h] - Display this help
[q] - Quit

解释如下:

[r] - Re-run all tests

This will re-run every test

[f] - Re-run failed tests

This will re-run every failing test

[b] - Toggle 'broken only' mode, where only failing tests are run

Broken only mode will only run tests that have previously failed, even if other tests would normally be affected by a code change. This can be useful if you are modifying code that is used by lots of tests, but you only want to focus on debugging the failing one.

[v] - Print failures from the last test run

Prints the failures to the console again, this can be useful if there has been lots of console output since the last run.

[p] - Pause tests

Temporarily stops running tests. This can be useful if you are making lots of changes, and don’t want feedback until they are all done.

[o] - Toggle test output

By default test output is filtered and not displayed on the console, so that test output and dev mode output is not interleaved. Enabling test output will print output to the console when tests are run. Even when output is disabled the filtered output is saved and can be viewed in the Dev UI.

[i] - Toggle instrumentation based reload

This is not directly related to testing, but allows you to toggle instrumentation based reload. This will allow live reload to avoid a restart if a change does not affect the structure of a class, which gives a faster reload and allows you to keep state.

[l] - Toggle live reload

This is not directly related to testing, but allows you to turn live reload on and off.

[s] - Force restart

This will force a scan for changed files, and will perform a live reload with and changes. Note that even if there are no changes the application will still restart. This will still work even if live reload is disabled.

Continuous Testing Without Dev Mode

无需开始开发模式即可运行持续测试。如果开发模式会干扰您的测试(例如,在同一端口上运行 wiremock),或者如果您只想使用测试进行开发,这会很有用。若要启动持续测试模式,请运行 mvn quarkus:test

在持续测试模式下运行时不可用开发用户界面,因为这是由开发模式提供的。

Multi-module Projects

注意持续测试支持模块化项目,因此在更改文件时,仍可以在应用程序之外的模块中运行测试。可按以下所列使用配置来控制所运行的模块。

默认情况下启用此选项,但可以通过 `quarkus.test.only-test-application-module=true`将其禁用。

Configuring Continuous Testing

持续测试支持多个配置选项,这些选项可用于限制运行的测试并控制输出。配置属性如下所示:

Unresolved include directive in modules/ROOT/pages/continuous-testing.adoc - include::../../../target/quarkus-generated-doc/config/quarkus-core_quarkus.test.adoc[]