Skip to content

Testing projectsテストプロジェクト


The test command (by default) uses pytest with select plugins and coverage.py. View the testing configuration for more information.test コマンド (デフォルトで) は、選択したプラグインと coverage.py を使用して pytest を利用します。詳細については テスト構成 を参照してください。

The majority of projects can be fully tested this way without the need for custom environments.ほとんどのプロジェクトは、この方法でカスタム 環境 を必要とせずに完全にテストできます。

Passing arguments引数の渡し方

When you run the test command without any arguments, tests is passed as the default argument to pytest (this assumes that you have a tests directory). For example, the following command invocation:test コマンドを引数なしで実行すると、testsデフォルト引数 として pytest に渡されます(これは tests ディレクトリがあることを前提としています)。例えば、次のコマンドの呼び出しは:

hatch test

would be translated roughly to:おおよそ次のように翻訳されます:

pytest tests

You can pass arguments to pytest by appending them to the test command. For example, the following command invocation:pytest に引数を渡すには、それらを test コマンドに追加します。例えば、次のコマンドの呼び出しは:

hatch test -vv tests/test_foo.py::test_bar

would be translated roughly to:おおよそ次のように翻訳されます:

pytest -vv tests/test_foo.py::test_bar

You can force the treatment of arguments as positional by using the -- separator, especially useful when built-in flags of the test command conflict with those of pytest, such as the --help flag. For example, the following command invocation:引数を位置引数として扱うよう強制するには、-- セパレーターを使用します。これは、test コマンドの組み込みフラグが pytest のフラグと衝突する場合、特に --help フラグのような場合に便利です。例えば、次のコマンドの呼び出しは:

hatch test -r -- -r fE -- tests

would be translated roughly to:おおよそ次のように翻訳されます:

pytest -r fE -- tests

Note注意

It's important to ensure that pytest receives an argument instructing what to run/where to locate tests. It's default behavior is . meaning that it will exhaustively search for tests in the current directory. This can not just be slow but also lead to unexpected behavior.重要なのは、pytestに何を実行するか、またはテストをどこに配置するかを指示する引数を渡すことです。デフォルトの動作は.であり、現在のディレクトリ内でテストを徹底的に検索します。これは遅くなるだけでなく、予期しない動作を引き起こす可能性もあります。

Environment selection環境選択

Single environment単一環境

If no environment options are selected, the test command will only run tests in the first defined environment that either already exists or is compatible. Additionally, the checking order will prioritize environments that define a version of Python that matches the interpreter that Hatch is running on.環境オプションが選択されていない場合、testコマンドは、既に存在するか互換性のある最初に定義された環境内のテストのみを実行します。さらに、チェックの順序は、Hatchが実行されているインタプリタと一致するPythonのバージョンを定義している環境を優先します。

For example, if you overrode the default matrix as follows:例えば、次のようにデフォルトのマトリックスをオーバーライドした場合:

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

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.11"]
feature = ["foo", "bar"]
[[envs.hatch-test.matrix]]
python = ["3.12", "3.11"]

[[envs.hatch-test.matrix]]
python = ["3.11"]
feature = ["foo", "bar"]

the expanded environments would normally be:拡張された環境は通常次のようになります:

hatch-test.py3.12
hatch-test.py3.11
hatch-test.py3.11-foo
hatch-test.py3.11-bar

If you install Hatch on Python 3.11, the checking order would be:Python 3.11にHatchをインストールした場合、チェックの順序は次のようになります:

hatch-test.py3.11
hatch-test.py3.11-foo
hatch-test.py3.11-bar
hatch-test.py3.12

Note注意

If you installed Hatch with an official installer or are using one of the standalone binaries, the version of Python that Hatch runs on is out of your control. If you are relying on the single environment resolution behavior, consider explicitly selecting environments based on the Python version instead.公式のインストーラーを使用してHatchをインストールした場合、またはスタンドアロンバイナリのいずれかを使用している場合、Hatchが実行されるPythonのバージョンはあなたの制御外です。単一環境の解決動作に依存している場合は、Pythonのバージョンに基づいて環境を明示的に選択することを検討してください。

All environmentsすべての環境

You can run tests in all compatible environments by using the --all flag. For example, say you defined the matrix and overrides as follows:すべての互換性のある環境でテストを実行するには、--allフラグを使用します。例えば、マトリックスとオーバーライドを次のように定義したとします:

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.12", "3.11"]
feature = ["foo", "bar"]

[tool.hatch.envs.hatch-test.overrides]
matrix.feature.platforms = [
  { value = "linux", if = ["foo", "bar"] },
  { value = "windows", if = ["foo"] },
  { value = "macos", if = ["bar"] },
]
[[envs.hatch-test.matrix]]
python = ["3.12", "3.11"]
feature = ["foo", "bar"]

[envs.hatch-test.overrides]
matrix.feature.platforms = [
  { value = "linux", if = ["foo", "bar"] },
  { value = "windows", if = ["foo"] },
  { value = "macos", if = ["bar"] },
]

The following table shows the environments in which tests would be run:以下の表は、テストが実行される環境を示しています:

Environment環境 LinuxLinux WindowsWindows macOSmacOS
hatch-test.py3.12-foohatch-test.py3.12-foo ✅ ✅ ❌
hatch-test.py3.12-barhatch-test.py3.12-bar ✅ ❌ ✅
hatch-test.py3.11-foohatch-test.py3.11-foo ✅ ✅ ❌
hatch-test.py3.11-barhatch-test.py3.11-bar ✅ ❌ ✅

Specific environments特定の環境

You can select subsets of environments by using the --include/-i and --exclude/-x options. These options may be used to include or exclude certain matrix variables, optionally followed by specific comma-separated values, and may be selected multiple times.--include/-i および --exclude/-x オプションを使用することで、環境のサブセットを選択できます。これらのオプションは、特定のマトリックス変数を含めたり除外したりするために使用でき、オプションで特定のカンマ区切りの値を続けることができ、複数回選択することも可能です。

For example, say you defined the matrix as follows:例えば、次のようにマトリックスを定義したとします:

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.12", "3.11"]
feature = ["foo", "bar", "baz"]
[[envs.hatch-test.matrix]]
python = ["3.12", "3.11"]
feature = ["foo", "bar", "baz"]

If you wanted to run tests in all environments that have Python 3.12 and either the foo or bar feature, you could use the following command invocation:Python 3.12を持つすべての環境でテストを実行したい場合、fooまたはbar機能のいずれかを持つ環境で、次のコマンドを使用できます:

hatch test -i python=3.12 -i feature=foo,bar

Alternatively, we could exclude the baz feature to achieve the same result:あるいは、同じ結果を得るために baz 機能を除外することもできます。

hatch test -i python=3.12 -x feature=baz

Tipヒント

Since selecting the version of Python is a common use case, you can use the --python/-py option as a shorthand. For example, the previous commands could have been written as:Python のバージョンを選択することは一般的な使用ケースであるため、--python/-py オプションをショートハンドとして使用できます。例えば、前のコマンドは次のように書くことができました:

hatch test -py 3.12 -i feature=foo,bar
hatch test -py 3.12 -x feature=baz

Measuring code coverageコードカバレッジの測定

You can enable code coverage by using the --cover flag. For example, the following command invocation:コードカバレッジ を有効にするには、--cover フラグを使用します。例えば、次のコマンドの呼び出し:

hatch test --cover

would be translated roughly to:はおおよそ次のように翻訳されます:

coverage run -m pytest tests

After tests run in all of the selected environments, the coverage data is combined and a report is shown. The --cover-quiet flag can be used to suppress the report and implicitly enables the --cover flag:すべての 選択された環境 でテストが実行された後、カバレッジデータが結合され、レポートが表示されます。--cover-quiet フラグを使用するとレポートを抑制でき、暗黙的に --cover フラグが有効になります:

hatch test --cover-quiet

Note注意

Coverage data files are generated at the root of the project. Be sure to exclude them from version control with the following glob-style pattern:カバレッジデータファイルはプロジェクトのルートに生成されます。次のグロブスタイルのパターンを使用して、バージョン管理から除外することを確認してください:

.coverage*

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

You can retry failed tests with the --retries option:失敗したテストを再試行するには、--retries オプションを使用します:

hatch test --retries 2

If a test fails every time and the number of retries is set to 2, the test will be run a total of three times.テストが毎回失敗し、再試行の回数が 2 に設定されている場合、テストは合計で三回実行されます。

You can also set the number of seconds to wait between retries with the --retry-delay option:再試行の間に待機する秒数を --retry-delay オプションで設定することもできます:

hatch test --retries 2 --retry-delay 1

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

You can parallelize test execution with the --parallel/-p flag:テスト実行を 並列化するには、--parallel/-p フラグを使用します:

hatch test --parallel

This distributes tests within an environment across multiple workers. The number of workers corresponds to the number of logical rather than physical CPUs that are available.これにより、環境内のテストが複数のワーカーに分散されます。ワーカーの数は、利用可能な物理CPUではなく論理CPUの数に対応します。

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

You can randomize the order of tests with the --randomize/-r flag:テストの順序を ランダム化するには、--randomize/-r フラグを使用します:

hatch test --randomize