pytestで1ファイル/1テストだけ実行したい(他よく使うオプション)


pytest実行時に便利なオプションを書いた備忘録です。

前提条件

例で出てくるプロジェクトのイメージ

pj_root(カレントディレクトリ)
  ├..
  └tests
    ├..
    └test_example.py

例で登場するtest_example.pyのイメージ

test_example.py
import unittest


class TestExample(unittest.TestCase):

    def test_example_ok_pattern(self):
        actual = True
        expected = True
        assert actual == expected

1ファイル/1テストだけ実行したい

1ファイルだけ実行する

pytest (path)

# 例
pytest tests/test_exapmle.py

1テストだけ実行する

pytest (path)::(TestClass)::(test_method)
pytest (path)::(test_method)

# 例
pytest tests/test_exapmle.py::TestExample::test_example_ok_pattern

その他、よく使うオプション

最後にコケたテストだけ実行する

pytest --lf

テストが1つでもコケたらそこでテスト実行を止める

pytest -x

テストがコケた時に結果を詳細に表示する

pytest -vv