Pythonのpytestで––collect-onlyオプションを試してみた
はじめに
この記事では、「pytestの––collect-onlyオプション(pytest ––collect-only
)を行うとどうなるのか」について書いています。
私はこれまでpytestを使ったことはほぼなく、pytestの勉強も兼ねて記事を書きました。
自分に向けた備忘録の意味もありますが、「pytest使いたいけどよく知らない」な人の一助になれば嬉しいです。
要点だけ
pytest ––collect-only
を実行すると、実行されるテストの一覧が表示される
前提(環境)
以下の環境、フォルダ構成で実験をしていきます。
sample ├ test_add.py └ test_sub.py
test_add.pyの中身は以下のようになっています。
def test_add1(): assert 3 == 1 + 2 def test_add2(): assert 4 == 1 + 3
また、test_sub.pyの中身は以下のようになっています。
def test_sub1(): assert 1 == 2 - 1 def test_sub2(): assert 2 == 3 - 1
pytest ––collect-only
をしてみる
上で示したsampleフォルダに対してpytest
を実行するとテスト結果が出力されますが、pytest ––collect-only
として実行すると以下が出力されます。
==================================== test session starts ===================================== platform win32 -- Python 3.9.4, pytest-7.1.3, pluggy-1.0.0 rootdir: C:\Users\xxxx\sample plugins: anyio-3.6.1 collected 4 items <Module test_add.py> <Function test_add1> <Function test_add2> <Module test_sub.py> <Function test_sub1> <Function test_sub2> ================================= 4 tests collected in 0.03s =================================
実行されるテストの一覧が表示されて、何がテストされる予定なのかが分かります。
Note
テスト自体は実行されるわけではないのでご注意ください
おわりに
「pytestの––collect-onlyオプション(pytest ––collect-only
)を行うとどうなるのか」について解説しました。
さいごに、記事を書く上で参考にしたサイトのリンクを以下に掲載します。合わせて読んでいただくと良いかと思います。