書籍の出典: Fang Lizhi Liang Lili 「pytest フレームワークと自動テスト アプリケーション」
先生の授業内容や実験ノートを勉強しながら整理してみんなで共有してください、違反しているものは削除されますのでよろしくお願いします!
要約投稿を添付してください: pytest フレームワーク高度な自習シリーズ | 要約
@pytest.mark.parametrize によるデータ駆動型。@pytest.mark.parametrize の基本的な機能は、テスト ケースを収集するプロセス中に、指定されたパラメーターに値を代入することで、マークされたオブジェクトの呼び出し (実行) を追加することです。次の例は、特定のパラメータ化でさまざまなデータを使用する方法を示しています。
単一のパラメトリック アプリケーション
一般的な使用シナリオ: テスト メソッド内の 1 つのデータのみが変更されます。つまり、複数のテスト データ セットがパラメーターを通じて渡されます。実行中、データの各セットが 1 回実行されます。
達成するための具体的な手順は次のとおりです。
(1) テストメソッドに @pytest.mark.parametrize を入力します。
(2) パラメータは 2 つあり、1 つはパラメータ名、もう 1 つはパラメータ値です。この値は複数指定でき、数値または文字を使用できます。
(3) テストメソッドのパラメータはparametrizeのパラメータ名と同じです。
(4) これらのデータをテスト メソッド内でパラメーター名を使用して呼び出します。
コードは以下のように表示されます。
import pytest
@pytest.mark.parametrize("test_case", [1,2,3,'orange','apple'])
def test_string(test_case):
print("\n要测试的数据:{}".format(test_case))
テストケースはデータの数だけ自動的に実行されます。実行結果は次のようになります。
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_mark_parametrize.py
Testing started at 14:01 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_parametrize.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 5 items
test_mark_parametrize.py::test_string[1] PASSED [ 20%]
要测试的数据:1
test_mark_parametrize.py::test_string[2] PASSED [ 40%]
要测试的数据:2
test_mark_parametrize.py::test_string[3] PASSED [ 60%]
要测试的数据:3
test_mark_parametrize.py::test_string[orange] PASSED [ 80%]
要测试的数据:orange
test_mark_parametrize.py::test_string[apple] PASSED [100%]
要测试的数据:apple
============================== 5 passed in 0.02s ==============================
Process finished with exit code 0
マルチパラメータアプリケーション
テストの入力データは式にすることができ、入力パラメーターは複数にすることができます。複数のデータをタプルで整理できます。以下は、計算機をテストする簡単な例です。最初の 2 つは変数で、後者は対応するデータです。3+5 は test_input パラメータ名に対応し、8 は予期されるパラメータ名に対応し、以降のデータも同様になります。eval は文字列 str を有効な式として評価し、結果を返します。
コードは以下のように表示されます。
import pytest
@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+5", 7), ("7*5", 30)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
データセットの 1 つを間違った形式で書き込み、アサーションの詳細を確認します。実行結果は以下の通りです。
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_mark_parametrize.py
Testing started at 14:03 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_parametrize.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 3 items
test_mark_parametrize.py::test_eval[3+5-8]
test_mark_parametrize.py::test_eval[2+5-7]
test_mark_parametrize.py::test_eval[7*5-30] PASSED [ 33%]PASSED [ 66%]FAILED [100%]
src\chapter-4\test_mark_parametrize.py:2 (test_eval[7*5-30])
35 != 30
Expected :30
Actual :35
<Click to see difference>
test_input = '7*5', expected = 30
@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+5", 7), ("7*5", 30)])
def test_eval(test_input, expected):
> assert eval(test_input) == expected
E assert 35 == 30
E +35
E -30
test_mark_parametrize.py:5: AssertionError
================================== FAILURES ===================================
______________________________ test_eval[7*5-30] ______________________________
test_input = '7*5', expected = 30
@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+5", 7), ("7*5", 30)])
def test_eval(test_input, expected):
> assert eval(test_input) == expected
E assert 35 == 30
E +35
E -30
test_mark_parametrize.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_mark_parametrize.py::test_eval[7*5-30] - assert 35 == 30
========================= 1 failed, 2 passed in 0.06s =========================
Process finished with exit code 1
複数のパラメータ化
ユースケースは複数の @pytest.mark.parametrize マーカーでマークできます。
コードは以下のように表示されます。
import pytest
@pytest.mark.parametrize('test_input', [1,2,3])
@pytest.mark.parametrize('test_output, expected', [(1,2), (3, 4)])
def test_multi(test_input, test_output, 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_multi.py
Testing started at 14:05 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_multi.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 6 items
test_multi.py::test_multi[1-2-1] PASSED [ 16%]
test_multi.py::test_multi[1-2-2] PASSED [ 33%]
test_multi.py::test_multi[1-2-3] PASSED [ 50%]
test_multi.py::test_multi[3-4-1] PASSED [ 66%]
test_multi.py::test_multi[3-4-2] PASSED [ 83%]
test_multi.py::test_multi[3-4-3] PASSED [100%]
============================== 6 passed in 0.02s ==============================
Process finished with exit code 0
パラメータ化とフィクスチャの組み合わせ
テスト メソッドが依存関係を注入する (つまり、フィクスチャを使用する) だけでなく、パラメータ化する必要がある場合、parametrize を使用すると競合が発生します。この場合、パラメータ化は、フィクスチャに付属するパラメータ params を通じて実現できます。これもパラメータ化の方法です。実装方法についてはセクション 3.8 を、アプリケーションについてはセクション 8.2.5 を参照してください。
pytestmark はパラメータ化を実装します
pytestmark に値を割り当てることで、テスト モジュールのパラメータ化を試みることができます。
コードは以下のように表示されます。
import pytest
pytestmark = pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)])
def test_module(test_input, expected):
assert test_input + 1 == expected
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_module.py
Testing started at 14:09 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_module.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_module.py::test_module[1-2] PASSED [ 50%]
test_module.py::test_module[3-4] PASSED [100%]
============================== 2 passed in 0.01s ==============================
Process finished with exit code 0