The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

継続的テスト

Quarkusアプリケーションで継続的テストを使用する方法について説明します。

1. 前提条件

このガイドを完成させるには、以下が必要です:

  • 約15分

  • IDE

  • JDK 17+がインストールされ、 JAVA_HOME が適切に設定されていること

  • Apache Maven 3.9.6

  • 使用したい場合は、 Quarkus CLI

  • ネイティブ実行可能ファイルをビルドしたい場合、MandrelまたはGraalVM(あるいはネイティブなコンテナビルドを使用する場合はDocker)をインストールし、 適切に設定していること

  • 入門ガイドに掲載されている、完了済のGreeterアプリケーション

2. はじめに

Quarkusは継続的テストをサポートしており、コードの変更が保存された直後にテストが実行されます。これにより、コードの変更に対するフィードバックを即座に得ることができます。Quarkusは、どのテストがどのコードをカバーしているかを検出し、この情報を使用して、コードが変更されたときに関連するテストのみを実行します。

3. ソリューション

入門ガイド アプリケーション(またはその他のアプリケーション)を起動します。

コマンドラインインタフェース
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.propertiesquarkus.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>

これを元に戻すと、テストが再び実行されます。

4. 継続的なテストのコントロール

連続テストをコントロールするために使用できる様々なホットキーがあります。 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

これは、すべてのテストを再実行します。

[f] - Re-run failed tests

これは、失敗したすべてのテストを再実行します。

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

Broken only モードでは、コードの変更によって他のテストが影響を受ける場合でも、 過去に失敗したテストのみを実行します。これは、多くのテストで使用されているコードを変更する際に、 失敗したテストのデバッグだけに集中したい場合などに便利です。

[v] - Print failures from the last test run

失敗をコンソールに再表示します。これは、前回の実行後に大量のコンソール出力があった場合に役立ちます。

[p] - Pause tests

テストの実行を一時的に停止します。これは、多くの変更を行っていて、すべてが完了するまでフィードバックを得たくない場合に便利です。

[o] - Toggle test output

デフォルトでは、テスト出力はフィルタリングされ、コンソールには表示されないので、テスト出力とdevモードの出力が混在することはありません。テスト出力を有効にすると、テストが実行されたときに出力がコンソールに表示されます。出力を無効にした場合でも、フィルタリングされた出力は保存され、開発UIで見ることができます。

[i] - Toggle instrumentation based reload

これはテストとは直接関係ありませんが、インスツルメンテーションベースのリロードを切り替えることができます。これにより、クラスの構造に影響を与えない変更の場合、再起動を避けるためにライブリロードが可能になり、リロードが速くなり、状態を維持することができます。

[l] - Toggle live reload

これはテストとは直接関係ありませんが、ライブリロードのオン/オフを切り替えることができます。

[s] - Force restart

これにより、変更されたファイルを強制的にスキャンし、変更に対してライブリロードを実行します。ライブリロードが無効になっていても、この機能は動作します。

5. 開発モードを使わない継続的なテスト

devモードを起動せずに、継続的なテストを行うことができます。これは、開発モードがテストの妨げになる場合 (同じポートで wiremock を実行している場合など) や、テストだけを使って開発したい場合などに便利です。継続的テストモードを開始するには、 mvn quarkus:test を実行します。

Dev UIは開発モードで提供されているため、継続的テストモードで実行している場合は利用できません。

6. マルチモジュールプロジェクト

継続的テストは、マルチモジュールのプロジェクトをサポートしているので、ファイルが変更された場合、アプリケーション以外のモジュールのテストが実行されることがあります。実行されるモジュールは、以下のように設定で制御できます。

これはデフォルトで有効になっており、 quarkus.test.only-test-application-module=true で無効にすることができます。

7. 継続的テストの設定

継続的テストでは、実行するテストを制限したり、出力を制御したりするために使用できる複数の設定オプションをサポートしています。設定項目は以下のとおりです。

ビルド時に固定される設定プロパティ - その他の設定プロパティは実行時にオーバーライド可能です。

Configuration property

デフォルト

If continuous testing is enabled. The default value is 'paused', which will allow you to start testing from the console or the Dev UI, but will not run tests on startup. If this is set to 'enabled' then testing will start as soon as the application has started. If this is 'disabled' then continuous testing is not enabled, and can’t be enabled without restarting the application.

Environment variable: QUARKUS_TEST_CONTINUOUS_TESTING

Show more

paused, enabled, disabled

paused

If output from the running tests should be displayed in the console.

Environment variable: QUARKUS_TEST_DISPLAY_TEST_OUTPUT

Show more

boolean

false

Tags that should be included for continuous testing. This supports JUnit Tag Expressions.

Environment variable: QUARKUS_TEST_INCLUDE_TAGS

Show more

list of string

Tags that should be excluded by default with continuous testing. This is ignored if include-tags has been set. Defaults to 'slow'. This supports JUnit Tag Expressions.

Environment variable: QUARKUS_TEST_EXCLUDE_TAGS

Show more

list of string

slow

Tests that should be included for continuous testing. This is a regular expression and is matched against the test class name (not the file name).

Environment variable: QUARKUS_TEST_INCLUDE_PATTERN

Show more

string

Tests that should be excluded with continuous testing. This is a regular expression and is matched against the test class name (not the file name). This is ignored if include-pattern has been set.

Environment variable: QUARKUS_TEST_EXCLUDE_PATTERN

Show more

string

.*\.IT[^.]+|.*IT|.*ITCase

Test engine ids that should be included for continuous testing.

Environment variable: QUARKUS_TEST_INCLUDE_ENGINES

Show more

list of string

Test engine ids that should be excluded by default with continuous testing. This is ignored if include-engines has been set.

Environment variable: QUARKUS_TEST_EXCLUDE_ENGINES

Show more

list of string

Changes tests to use the 'flat' ClassPath used in Quarkus 1.x versions. This means all Quarkus and test classes are loaded in the same ClassLoader, however it means you cannot use continuous testing. Note that if you find this necessary for your application then you may also have problems running in development mode, which cannot use a flat class path.

Environment variable: QUARKUS_TEST_FLAT_CLASS_PATH

Show more

boolean

false

The profile to use when testing the native image

Environment variable: QUARKUS_TEST_NATIVE_IMAGE_PROFILE

Show more

string

prod

The profile to use when testing using @QuarkusIntegrationTest

Environment variable: QUARKUS_TEST_INTEGRATION_TEST_PROFILE

Show more

string

prod

A comma separated list of profiles (dev, test, prod or custom profiles) to use when testing using @QuarkusTest

Environment variable: QUARKUS_TEST_PROFILE

Show more

list of string

test

The tags this profile is associated with. When the quarkus.test.profile.tags System property is set (its value is a comma separated list of strings) then Quarkus will only execute tests that are annotated with a @TestProfile that has at least one of the supplied (via the aforementioned system property) tags.

Environment variable: QUARKUS_TEST_PROFILE_TAGS

Show more

list of string

Controls the container network to be used when @QuarkusIntegration needs to launch the application in a container. This setting only applies if Quarkus does not need to use a shared network - which is the case if DevServices are used when running the test.

Environment variable: QUARKUS_TEST_CONTAINER_NETWORK

Show more

string

Additional launch parameters to be used when Quarkus launches the produced artifact for @QuarkusIntegrationTest When the artifact is a jar, this string is passed right after the java command. When the artifact is a container, this string is passed right after the docker run command. When the artifact is a native binary, this string is passed right after the native binary name.

Environment variable: QUARKUS_TEST_ARG_LINE

Show more

list of string

Used in @QuarkusIntegrationTest to determine how long the test will wait for the application to launch

Environment variable: QUARKUS_TEST_WAIT_TIME

Show more

Duration

PT1M

Configures the hang detection in @QuarkusTest. If no activity happens (i.e. no test callbacks are called) over this period then QuarkusTest will dump all threads stack traces, to help diagnose a potential hang. Note that the initial timeout (before Quarkus has started) will only apply if provided by a system property, as it is not possible to read all config sources until Quarkus has booted.

Environment variable: QUARKUS_TEST_HANG_DETECTION_TIMEOUT

Show more

Duration

10M

The type of test to run, this can be either: quarkus-test: Only runs @QuarkusTest annotated test classes unit: Only runs classes that are not annotated with @QuarkusTest all: Runs both, running the unit tests first

Environment variable: QUARKUS_TEST_TYPE

Show more

unit, quarkus-test, all

all

If a class matches this pattern then it will be cloned into the Quarkus ClassLoader even if it is in a parent first artifact. This is important for collections which can contain objects from the Quarkus ClassLoader, but for most parent first classes it will just cause problems.

Environment variable: QUARKUS_TEST_CLASS_CLONE_PATTERN

Show more

string

java\..*

If this is true then only the tests from the main application module will be run (i.e. the module that is currently running mvn quarkus:dev). If this is false then tests from all dependency modules will be run as well.

Environment variable: QUARKUS_TEST_ONLY_TEST_APPLICATION_MODULE

Show more

boolean

false

Modules that should be included for continuous testing. This is a regular expression and is matched against the module groupId:artifactId.

Environment variable: QUARKUS_TEST_INCLUDE_MODULE_PATTERN

Show more

string

Modules that should be excluded for continuous testing. This is a regular expression and is matched against the module groupId:artifactId. This is ignored if include-module-pattern has been set.

Environment variable: QUARKUS_TEST_EXCLUDE_MODULE_PATTERN

Show more

string

If the test callbacks should be invoked for the integration tests (tests annotated with @QuarkusIntegrationTest).

Environment variable: QUARKUS_TEST_ENABLE_CALLBACKS_FOR_INTEGRATION_TESTS

Show more

boolean

false

Set additional ports to be exposed when @QuarkusIntegration needs to launch the application in a container.

Environment variable: QUARKUS_TEST_CONTAINER_ADDITIONAL_EXPOSED_PORTS

Show more

Map<String,String>

A set of labels to add to the launched container

Environment variable: QUARKUS_TEST_CONTAINER_LABELS

Show more

Map<String,String>

A set of volume mounts to add to the launched container

Environment variable: QUARKUS_TEST_CONTAINER_VOLUME_MOUNTS

Show more

Map<String,String>

Additional environment variables to be set in the process that @QuarkusIntegrationTest launches.

Environment variable: QUARKUS_TEST_ENV

Show more

Map<String,String>

期間フォーマットについて

To write duration values, use the standard java.time.Duration format. See the Duration#parse() Java API documentation for more information.

数字で始まる簡略化した書式を使うこともできます:

  • 数値のみの場合は、秒単位の時間を表します。

  • 数値の後に ms が続く場合は、ミリ秒単位の時間を表します。

その他の場合は、簡略化されたフォーマットが解析のために java.time.Duration フォーマットに変換されます:

  • 数値の後に hms が続く場合は、その前に PT が付けられます。

  • 数値の後に d が続く場合は、その前に P が付けられます。

関連コンテンツ