書籍の出典: Fang Lizhi Liang Lili 「pytest フレームワークと自動テスト アプリケーション」
先生の授業内容や実験ノートを勉強しながら整理してみんなで共有してください、違反しているものは削除されますのでよろしくお願いします!
要約投稿を添付してください: pytest フレームワーク高度な自習シリーズ | 要約
idsパラメータはidですが、キーワードと同じで使用できないため、idsに変更します。通常、idsを記述しない場合は毎回異なるデータ、つまりデータそのものが表示されますが、idsの値が定義されている場合はその値が表示されます。ID にコンテンツを書き込むことで、テスト ポイントをマークできます。通常、テスト中に数値、文字、境界値などをテストするため、このパラメーターを設定することで、カバレッジが包括的であるかどうかを確認できます。たとえば、最初のデータは数字、2 番目のデータは中国語、3 番目のデータは特殊文字です。このようにして、レポートの結果を確認するときに、テストが完了したかどうかを確認できます。
ids は、テスト ID を生成するために使用される実行可能オブジェクト、または新しく追加されたすべてのテスト ケースのテスト ID を示すリスト/タプルです。これらの ID を -k とともに使用すると、実行する特定のユース ケースを選択でき、失敗したときにその特定のユース ケースも識別されます。pytest --collect-only を実行すると、生成された ID が表示されます。
IDの長さ
テスト ID がリスト/タプルを使用して直接指定された場合、その長さは argvalue の長さと同じになります。
コードは以下のように表示されます。
import pytest
@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['first','second'])
def test_ids_with_ids(input, expected):
pass
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py
Testing started at 9:30 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 2 items
test_test.py::test_ids_with_ids[first] PASSED [ 50%]
test_test.py::test_ids_with_ids[second] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0
入力パラメータの ID は最初、最初の値は 1、2 回目の値は 3、予期されるパラメータの ID は 2 番目、初回の値は 2、そして2回目は4です。
同じID
テスト ID が同じ場合、pytest は後で [num0] や [num1] などのインデックスを自動的に追加します。
import pytest
@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['num', 'num'])
def test_ids_with_ids(input, expected):
pass
実行結果は次のようになります。
\D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py
Testing started at 9:31 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 2 items
test_test.py::test_ids_with_ids[num0] PASSED [ 50%]
test_test.py::test_ids_with_ids[num1] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0
ID に中国語を使用する
テストIDには中国語も使用でき、デフォルトではバイト列が表示されます。
import pytest
@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['num', '中文'])
def test_ids_with_ids(input, expected):
pass
収集されるテスト ID は次のとおりです。
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py
Testing started at 9:31 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 2 items
test_test.py::test_ids_with_ids[num] PASSED [ 50%]
test_test.py::test_ids_with_ids[\u4e2d\u6587] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0
上記の結果からわかるように、「中国語」が表示されることが想定されていますが、実際には \u4e2d\u6587 が表示されます。この問題を解決する方法として、著者はソースコード python.py を確認しました。
中国語の文字化けを解決するには、pytest.ini で disable_test_id_escaping_and_forfeit_all_rights_to_community_support オプションを True に設定します。
コードは以下のように表示されます。
[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True
再収集されたテストIDは以下のとおりです。
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py
Testing started at 9:33 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collecting ... collected 2 items
test_test.py::test_ids_with_ids[num] PASSED [ 50%]
test_test.py::test_ids_with_ids[中文] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0
機能ごとにIDを生成する
import pytest
def idfn(val):
return val + 1
@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=idfn)
def test_ids_with_ids(input, expected):
pass
実行結果は以下のように表示されます。
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py
Testing started at 9:34 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collecting ... collected 2 items
test_test.py::test_ids_with_ids[2-3] PASSED [ 50%]
test_test.py::test_ids_with_ids[4-5] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0
上記の例から、特定の引数値パラメータ (1, 2) について、それが 1 と 2 に分割されてそれぞれ idfn に渡され、戻り値が - 記号で結合されていることを理解するのは難しくありません。 as a 全体として (1, 2) を渡すのではなく、テスト ID が返されます。
ID カバレッジ
上記のソース コードから、id 属性が pytest.param で指定されている場合、ids 内の対応するテスト ID が上書きされることもわかります。
コードは以下のように表示されます。
import pytest
@pytest.mark.parametrize('input, expected', [(1,2),pytest.param(3,4,id='id_via_pytest_param')], ids=['first', 'second'])
def test_ids_with_ids(input, expected):
pass
実行結果は以下の通りです。
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py
Testing started at 9:38 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collecting ... collected 2 items
test_test.py::test_ids_with_ids[first] PASSED [ 50%]
test_test.py::test_ids_with_ids[id_via_pytest_param] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0
テスト ID は 2 番目ではなく、id_via_pytest_param です。
IDの役割
id の主な機能は、テスト ケースをさらに絞り込み、さまざまなテスト シナリオを区別し、ターゲットを絞ったテストの実行のための新しい方法を提供することです。
たとえば、次のテスト ケースでは、-k 'Non-Windows' オプションを渡して、非 Windows に関連するシナリオのみを実行できます。
コードは以下のように表示されます。
import pytest
@pytest.mark.parametrize('input, expected',[
pytest.param(1,2,id='Windows'),
pytest.param(3,4,id='Windows'),
pytest.param(5,6,id='Non-Windows')
])
def test_ids_with_ids(input, expected):
pass
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4> pytest -k 'Non-Windows' .\test_ids.py
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collected 3 items / 2 deselected / 1 selected
test_ids.py . [100%]
=================================================================================================== 1 passed, 2 deselected in 0.01s ===================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4>