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.9

  • 使用したい場合は、 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 あるいは gradle quarkusTest を実行します。

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

6. 実行するテストの選択

quarkus.test.include-patternquarkus.test.exclude-pattern の設定プロパティを使用して、実行するテストを選択することができます。 これらはテストクラスの完全修飾クラス名とマッチする正規表現です。 include-patterns が設定されている場合、 exclude-patterns は無視されます。

あるいは、ビルドシステム固有のアプローチを使用することもできます。 Maven では -Dtest=…​ システムプロパティ、Gradle では --tests …​ コマンドラインオプションです。 これらのオプションは、 maven quarkus:[dev|test] または gradle quarkus[Dev|Test] と一緒に使用すると、 mvn test または gradle test と同じように動作します。 これらのオプションを使用すると、 quarkus.test.[include|exclude]-pattern の設定は無視されます。

6.1. Maven

-Dtest=…​ システムプロパティは、継続的テストのために指定されたテストを選択します。 この設定プロパティの形式は、Maven Surefire -Dtest=…​形式 と同じです。 具体的には、クラス・ファイル・パスおよび/またはメソッド名のグロブのカンマ ( , ) 区切りリストです。各グロブの先頭に感嘆符 ( ! ) を付けることができます。除外は、包含よりも優先度が高くなります。クラス・ファイル・パス・グロブは、ハッシュ記号 ( # ) でメソッド名グロブと区切られ、プラス記号 ( + ) で区切られた複数のメソッド名グロブが存在する可能性があります。

例えば、以下のようになります。

  • Basic* : Basic で始まるすべてのクラス。

  • ???Test : 任意の3文字の後に Test

  • !Unstable* : Unstable で始まるクラスを除くすべてのクラス。

  • pkg/**/Ci*leTest: パッケージ pkg とサブパッケージ内の、Ci で始まり leTest で終わるすべてのクラス。

  • *Test#test*One+testTwo?????: Test で終わるすべてのクラスで、その中の test で始まり One で終わるメソッド、または testTwo で始まり 5 文字の任意の文字が続くメソッドのみ

  • #fast*+slowTest: すべてのクラスの中で fast で始まるメソッドまたは slowTest という名前のメソッドのみ

構文 %regex[...] および %ant[...] はサポートされていないことに注意してください。

6.2. Gradle

--tests …​ コマンドラインオプションは、継続的テストのために指定されたテストを選択します。 書式は gradle test --tests …​ 同じです。具体的には: オプションは複数回渡すことができ、それぞれの項目はテストのクラス名とオプションでメソッド名を表す単純なパターンです。 パターンが大文字で始まる場合はクラスの単純な名前にマッチし、そうでない場合はクラスの完全修飾名にマッチします。ピリオド ( . ) で区切られたクラス名の後に、メソッド名のパターンを含めることができます。ワイルドカード文字は だけです。 は純粋にテキストに基づいており、パッケージの区切り文字には特に注意を払っていないことに注意してください。

例えば、以下のようになります。

  • com.example.Basic*: パッケージ com.example 内の Basic で始まるすべてのクラス

  • MyTest*: シンプル名が MyTest で始まるすべてのクラス

  • *.pkg.Test*: パッケージ pkg 内のすべてのクラス (親パッケージに関係なく) で、Test で始まります。

  • MyTest.test*: 単純な名前が MyTest であるすべてのクラス、およびその中で test で始まるメソッドのみ

  • com.example.IntegTest.fast*: クラス com.example.IntegTest、およびその中の fast で始まるメソッドのみ

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

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

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

8. 継続的テストの設定

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

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

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

ブーリアン

false

The FQCN of the JUnit ClassOrderer to use. If the class cannot be found, it fallbacks to JUnit default behaviour which does not set a ClassOrderer at all.

Environment variable: QUARKUS_TEST_CLASS_ORDERER

Show more

string

io.quarkus.test.junit.util.QuarkusTestProfileAwareClassOrderer

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

ブーリアン

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

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

Environment variable: QUARKUS_TEST_CONTAINER_ADDITIONAL_EXPOSED_PORTS__HOST_PORT_

Show more

Map<String,String>

A set of labels to add to the launched container

Environment variable: QUARKUS_TEST_CONTAINER_LABELS__LABEL_NAME_

Show more

Map<String,String>

A set of volume mounts to add to the launched container

Environment variable: QUARKUS_TEST_CONTAINER_VOLUME_MOUNTS__HOST_PATH_

Show more

Map<String,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

string

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

Environment variable: QUARKUS_TEST_ENV__ENVIRONMENT_VARIABLE_NAME_

Show more

Map<String,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 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

ブーリアン

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

ブーリアン

false

Used to override the artifact type against which a @QuarkusIntegrationTest or @QuarkusMainIntegrationTest run. For example, if the application’s artifact is a container build from a jar, this property could be used to test the jar instead of the container.

Allowed values are: jar, native

Environment variable: QUARKUS_TEST_INTEGRATION_TEST_ARTIFACT_TYPE

Show more

string

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

期間の値を書くには、標準の java.time.Duration フォーマットを使います。 詳細は Duration#parse() Java API documentation を参照してください。

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

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

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

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

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

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

関連コンテンツ