Pythonのpytestで-kオプションを試してみる

はじめに

この記事では、「Pythonにあるpytestの-kオプションに関する概要と実際の簡単な使い方」について書いています。

要点だけ

  • 指定した文字列で検索して、テスト実行する
  • pytest -k "xxx"と、""の間に文字列を記述する
  • 「特定のテストだけ実施したい」場合に便利な機能

実際に試してみる

Note

試した際の環境は以下の通りです。

実験で使うフォルダ(ファイル)は以下の通りです。

sample
├ test_add.py
└ test_sub.py

test_add.pyの中身は以下のようになっています。

def test_add1():
        assert 2 == 1 + 2

def test_add2():
        assert 4 == 1 + 3

test_add1()は、テストに失敗するようにしている点にご注意ください

また、test_sub.pyの中身は以下のようになっています。

def test_sub1():
        assert 1 == 2 - 1

def test_sub2():
        assert 2 == 3 - 1

この状態で、まずはpytestした際の出力結果を見てみましょう

================================================= test session starts =================================================
platform win32 -- Python 3.9.4, pytest-7.1.3, pluggy-1.0.0
rootdir: C:\Users\xxx\sample
plugins: anyio-3.6.1
collected 4 items

test_add.py F.                                                                                                   [ 50%]
test_sub.py ..                                                                                                   [100%]

====================================================== FAILURES =======================================================
______________________________________________________ test_add1 ______________________________________________________

    def test_add1():
>           assert 2 == 1 + 2
E           assert 2 == (1 + 2)

test_add.py:2: AssertionError
=============================================== short test summary info ===============================================
FAILED test_add.py::test_add1 - assert 2 == (1 + 2)
============================================= 1 failed, 3 passed in 0.23s =============================================

4つあるテスト全てが実施され、1つが失敗、3つが成功したと書かれています。

では次に-kオプションを使い、一部のテストだけ実施してみましょう。

例えば、pytest -k "2"として、関数に"2"とつくテストだけ(つまり、test_add.pytest_add2()test_sub.pytest_sub2()の2つを行う)やってみるようにしてみましょう。

以下のような出力が得られます。

================================================= test session starts =================================================
platform win32 -- Python 3.9.4, pytest-7.1.3, pluggy-1.0.0
rootdir: C:\Users\xxx\sample
plugins: anyio-3.6.1
collected 4 items / 2 deselected / 2 selected

test_add.py .                                                                                                    [ 50%]
test_sub.py .                                                                                                    [100%]

=========================================== 2 passed, 2 deselected in 0.04s ===========================================

pytestだけの時と違い、2つだけテストが実施されています。が、これでは何のテストが実施されているか分かりませんので、pytest -k "2" --collect-onlyとし、テストされる関数を確認してみます。

================================================= test session starts =================================================
platform win32 -- Python 3.9.4, pytest-7.1.3, pluggy-1.0.0
rootdir: C:\Users\xxx\sample
plugins: anyio-3.6.1
collected 4 items / 2 deselected / 2 selected

<Module test_add.py>
  <Function test_add2>
<Module test_sub.py>
  <Function test_sub2>

===================================== 2/4 tests collected (2 deselected) in 0.03s =====================================

期待していたtest_add2()test_sub2()だけテストされていることが分かりました。

おわりに

Pythonにあるpytestの-kオプションに関する概要と実際の簡単な使い方」について解説しました。

さいごに、記事を書く上で参考にしたサイトのリンクを以下に掲載します。合わせて読んでいただくと良いかと思います。

https://ya6mablog.com/how-to-use-pytest-option/