Skip to content

Testing configurationテスト設定


Check out the testing overview tutorial for a more comprehensive walk-through.テスト概要チュートリアルをチェックして、より包括的な手順を確認してください。

Settings設定

If an option has a corresponding test command flag, the flag will always take precedence.オプションに対応するtestコマンドフラグがある場合、そのフラグが常に優先されます。

Default argumentsデフォルト引数

You can define default arguments for the test command by setting the default-args option, which must be an array of strings. The following is the default configuration:testコマンドのデフォルト引数を設定するには、default-argsオプションを設定します。これは文字列の配列でなければなりません。以下がデフォルト設定です:

[tool.hatch.envs.hatch-test]
default-args = ["tests"]
[envs.hatch-test]
default-args = ["tests"]

Extra arguments追加引数

You can define extra internal arguments for test scripts by setting the extra-args option, which must be an array of strings. For example, if you wanted to increase the verbosity of pytest, you could set the following:テストスクリプトのために追加の内部引数を定義するには、extra-argsオプションを設定します。これは文字列の配列でなければなりません。例えば、pytestの冗長性を増やしたい場合、次のように設定できます:

[tool.hatch.envs.hatch-test]
extra-args = ["-vv"]
[envs.hatch-test]
extra-args = ["-vv"]

Randomize test orderテスト順序をランダム化

You can randomize the order of tests by enabling the randomize option which corresponds to the --randomize/-r flag:テストの順序をランダム化するには、randomizeオプションを有効にします。これは--randomize/-rフラグに対応しています:

[tool.hatch.envs.hatch-test]
randomize = true
[envs.hatch-test]
randomize = true

Parallelize test executionテスト実行を並列化

You can parallelize test execution by enabling the parallel option which corresponds to the --parallel/-p flag:テスト実行を並列化するには、parallelオプションを有効にします。これは--parallel/-pフラグに対応しています:

[tool.hatch.envs.hatch-test]
parallel = true
[envs.hatch-test]
parallel = true

Retry failed tests失敗したテストを再試行

You can retry failed tests by setting the retries option which corresponds to the --retries flag:失敗したテストを再試行するには、retriesオプションを設定します。これは--retriesフラグに対応しています:

[tool.hatch.envs.hatch-test]
retries = 2
[envs.hatch-test]
retries = 2

You can also set the number of seconds to wait between retries by setting the retry-delay option which corresponds to the --retry-delay flag:再試行の間に待機する秒数を設定することもできます。これはretry-delayオプションを設定することで行います。これは--retry-delayフラグに対応しています:

[tool.hatch.envs.hatch-test]
retry-delay = 1
[envs.hatch-test]
retry-delay = 1

Customize environment環境をカスタマイズ

You can fully alter the behavior of the environment used by the test command.testコマンドで使用される環境の動作を完全に変更できます。

Dependencies依存関係

You can define extra dependencies that your tests may require:テストに必要な追加の依存関係を定義できます:

[tool.hatch.envs.hatch-test]
extra-dependencies = [
  "pyfakefs",
  "pytest-asyncio",
  "pytest-benchmark",
  "pytest-memray",
  "pytest-playwright",
  "pytest-print",
]
[envs.hatch-test]
extra-dependencies = [
  "pyfakefs",
  "pytest-asyncio",
  "pytest-benchmark",
  "pytest-memray",
  "pytest-playwright",
  "pytest-print",
]

The following is the default configuration:以下はデフォルトの設定です:

[tool.hatch.envs.hatch-test]
dependencies = [
  "coverage-enable-subprocess==1.0",
  "coverage[toml]~=7.11",
  "pytest~=9.0",
  "pytest-mock~=3.12",
  "pytest-randomly~=3.15",
  "pytest-rerunfailures~=14.0",
  "pytest-xdist[psutil]~=3.5",
]
[envs.hatch-test]
dependencies = [
  "coverage-enable-subprocess==1.0",
  "coverage[toml]~=7.11",
  "pytest~=9.0",
  "pytest-mock~=3.12",
  "pytest-randomly~=3.15",
  "pytest-rerunfailures~=14.0",
  "pytest-xdist[psutil]~=3.5",
]

MatrixMatrix

You can override the default series of matrices:デフォルトの一連のマトリックスを上書きできます:

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.14", "3.13", "3.12", "3.11", "3.10"]
[[envs.hatch-test.matrix]]
python = ["3.14", "3.13", "3.12", "3.11", "3.10"]

Scriptsスクリプト

If you want to change the default commands that are executed, you can override the scripts. The following default scripts must be redefined:実行されるデフォルトのコマンドを変更したい場合は、スクリプトを上書きできます。以下のデフォルトスクリプトは再定義する必要があります:

[tool.hatch.envs.hatch-test.scripts]
run = "pytest{env:HATCH_TEST_ARGS:} {args}"
run-cov = "coverage run -m pytest{env:HATCH_TEST_ARGS:} {args}"
cov-combine = "coverage combine"
cov-report = "coverage report"
[envs.hatch-test.scripts]
run = "pytest{env:HATCH_TEST_ARGS:} {args}"
run-cov = "coverage run -m pytest{env:HATCH_TEST_ARGS:} {args}"
cov-combine = "coverage combine"
cov-report = "coverage report"

The run script is the default behavior while the run-cov script is used instead when measuring code coverage. The cov-combine script runs after all tests complete when measuring code coverage, as well as the cov-report script when not using the --cover-quiet flag.runスクリプトはデフォルトの動作であり、run-covスクリプトはコードカバレッジを測定する際に代わりに使用されます。cov-combineスクリプトは、コードカバレッジを測定する際にすべてのテストが完了した後に実行され、--cover-quietフラグを使用しない場合はcov-reportスクリプトも実行されます。

Note注意

The HATCH_TEST_ARGS environment variable is how the test command's flags are translated and internally populated without affecting the user's arguments. This is also the way that extra arguments are passed.HATCH_TEST_ARGS環境変数は、testコマンドのフラグがどのように変換され、ユーザーの引数に影響を与えずに内部的に設定されるかを示します。これは、追加の引数が渡される方法でもあります。

Installerインストーラー

By default, UV is enabled. You may disable that behavior as follows:デフォルトでは、UVが有効です。この動作を無効にするには、次のようにします:

[tool.hatch.envs.hatch-test]
installer = "pip"
[envs.hatch-test]
installer = "pip"