継続的テスト
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
./mvnw quarkus:dev
./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>
これを元に戻すと、テストが再び実行されます。
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-pattern
と quarkus.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: Show more |
|
|
If output from the running tests should be displayed in the console. Environment variable: Show more |
ブーリアン |
|
The FQCN of the JUnit Environment variable: Show more |
string |
|
Tags that should be included for continuous testing. This supports JUnit Tag Expressions. Environment variable: 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: Show more |
list of string |
|
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: 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: Show more |
string |
|
Test engine ids that should be included for continuous testing. Environment variable: 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: 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: Show more |
ブーリアン |
|
The profile to use when testing the native image Environment variable: Show more |
string |
|
The profile to use when testing using Environment variable: Show more |
string |
|
A comma separated list of profiles (dev, test, prod or custom profiles) to use when testing using @QuarkusTest Environment variable: Show more |
list of string |
|
The tags this profile is associated with. When the Environment variable: 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: Show more |
string |
|
Set additional ports to be exposed when @QuarkusIntegration needs to launch the application in a container. Environment variable: Show more |
Map<String,String> |
|
A set of labels to add to the launched container Environment variable: Show more |
Map<String,String> |
|
A set of volume mounts to add to the launched container Environment variable: Show more |
Map<String,String> |
|
Additional launch parameters to be used when Quarkus launches the produced artifact for Environment variable: Show more |
string |
|
Additional environment variables to be set in the process that Environment variable: Show more |
Map<String,String> |
|
Used in Environment variable: Show more |
|
|
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: Show more |
|
|
The type of test to run, this can be either: quarkus-test: Only runs Environment variable: Show more |
|
|
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: Show more |
ブーリアン |
|
Modules that should be included for continuous testing. This is a regular expression and is matched against the module groupId:artifactId. Environment variable: 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: Show more |
string |
|
If the test callbacks should be invoked for the integration tests (tests annotated with Environment variable: Show more |
ブーリアン |
|
Used to override the artifact type against which a Allowed values are: jar, native Environment variable: Show more |
string |
期間フォーマットについて
期間の値を書くには、標準の 数字で始まる簡略化した書式を使うこともできます:
その他の場合は、簡略化されたフォーマットが解析のために
|