Skip to content

Testing configuration


より包括的なウォークスルーについては、testing overview tutorialを参照してください。

Settings

オプションに対応するtestコマンドフラグがある場合、そのフラグが常に優先されます。

Default arguments

testコマンドのデフォルトの引数を定義するには、default-argsオプションを設定します。このオプションは文字列の配列である必要があります。デフォルト設定は次のとおりです。:

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

Extra arguments

testscriptsに追加の内部引数を定義するには、文字列の配列であるextra-argsオプションを設定します。たとえば、pytestの冗長性を高めるには、次のように設定します。:

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

Randomize test order

--randomize/-rフラグに対応するrandomizeオプションを有効にすることで、テストの順序をrandomizeすることができます。:

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

Parallelize test execution

--parallel/-pフラグに対応するparallelオプションを有効にすることで、parallelizeテストを実行できます。:

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

Retry failed tests

--retriesフラグに対応するretriesオプションを設定することで、失敗したテストをretryすることができます。:

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

--retry-delayフラグに対応するretry-delayオプションを設定することで、再試行の間に待機する秒数を設定することもできます。:

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

Customize environment

testコマンドで使用される環境の動作を完全に変更できます。

Dependencies

テストに必要なextra dependenciesを定義できます。:

[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",
]

次に、デフォルト設定を示します。:

[tool.hatch.envs.hatch-test]
dependencies = [
  "coverage-enable-subprocess==1.0",
  "coverage[toml]~=7.4",
  "pytest~=8.1",
  "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.4",
  "pytest~=8.1",
  "pytest-mock~=3.12",
  "pytest-randomly~=3.15",
  "pytest-rerunfailures~=14.0",
  "pytest-xdist[psutil]~=3.5",
]

Matrix

デフォルトの一連のmatricesは上書きできます。:

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

Scripts

実行されるデフォルト・コマンドを変更する場合は、scriptsを上書きできます。次のデフォルト・スクリプトを再定義する必要があります:

[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"

runスクリプトはデフォルトの動作ですが、コードカバレッジを測定する場合は代わりにrun-covスクリプトが使用されます。コードカバレッジを測定する場合はすべてのテストが完了した後にcov-combineスクリプトが実行され、--cover-quietフラグを使用しない場合はcov-reportスクリプトが実行されます。

Note

HATCH_TEST_ARGS環境変数は、ユーザの引数に影響を与えることなく、testコマンドのフラグが変換され、内部的に入力される方法です。これは、extra argumentsが渡される方法でもあります。

Installer

既定では、UV is enabledです。この動作を無効にするには、次の手順を実行します。:

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