この記事は、「新人クリエーションセレモニー」イベントに参加し、一緒にゴールドクリエーションの道を歩み始めました。
2.13スキップとxfailは失敗したテストケースを処理します
特定のプラットフォームで機能しない、または失敗したいテスト関数にマークを付けて、pytestがそれに応じて処理できるようにし、テストスイートを緑色に保ちながら、テストセッションの概要をレンダリングします。スキップすると、特定の場合にのみ実行できます。条件が満たされているのは、テストに合格することだけです。そうでない場合、pytestはテストの実行を完全にスキップする必要があります。
一般的な例は、Windows以外のプラットフォームでWindowsのみのテストをスキップすることです。xfailは、何らかの理由でテストが失敗すると予想されることを意味します。もう1つの一般的な例は、まだ実装されていない機能やまだ修正されていないバグのテストです。
テストに合格すると、予想される失敗(pytest.mark.xfailでマークされている)にもかかわらず、それはxpassであり、テストの概要で報告されます。
2.13.1テストケースのスキップ
1.デコレータはスキップをマークし、スキップ理由を渡します。
import pytest
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
...
复制代码
2.pytest.skip必須スキップ
def test_function():
if not valid_config():
pytest.skip("unsupported configuration")
复制代码
3.パラメーター化のユースケースをスキップします。
import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
pytest.skip("跳过用例")
复制代码
4.スキッププラットフォームはすべて勝利のユースケースです
import sys
import pytest
if not sys.platform.startswith("win"):
pytest.skip("skipping windows-only tests", allow_module_level=True)
复制代码
5.条件付きスキップのユースケース
条件付きで何かをスキップしたい場合は、Python3.10より前のインタープリターで実行するときにスキップするようにマークされたテスト関数の例を次に示します。
import sys
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher˓→")
def test_function():
...
复制代码
6.特定の条件に基づいて特定のテストケースをスキップする
モジュール(pyファイル)間でskipifフラグを共有して、特定の条件に基づいてモジュール内の特定のテストをスキップします。
# content of test_mymodule.py
import mymodule
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1, 1), reason="at least mymodule-1.1 required" )
@minversion
def test_function():
pass
复制代码
# test_myothermodule.py
from test_mymodule import minversion
@minversion
def test_anotherfunction():
pass
复制代码
7.クラスをスキップするには、クラスでskipifタグを使用します
import pytest, sys
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
class TestPosixCalls:
def test_function(self):
"will not be setup or run under 'win32' platform"
复制代码
8.モジュールのすべてのテスト機能をスキップするには、グローバルテストタグを使用できます
import pytest
# test_module.py
pytestmark = pytest.mark.skipif(...)
复制代码
9.ファイルまたはディレクトリをスキップします。pytestを使用してから、対応するディレクトリのユースケースを使用します。
2.13.2テストケースを失敗としてマークする
1. xfailマーカーを使用して、テストを失敗させることを示します。
@pytest.mark.xfail
def test_function():
...
复制代码
2.テスト実行中にセットアップが失敗しました
pytest.xfail()の呼び出し後、他のコードは実行されません。これは、既知の例外をスローすることによって内部的にこれを行うためです。
import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
pytest.xfail("预期失败")
复制代码
3.特定の条件下での障害、原因
@pytest.mark.xfail(sys.platform == "win32", reason="bug in a 3rd party library")
def test_function():
...
复制代码
4.マークされたユースケースの実行を禁止する
@pytest.mark.xfail(run=False)
def test_function():
...
复制代码
5. strictを設定して、xfailのテストケースの実行結果を失敗として作成します
import pytest
@pytest.mark.xfail(strict=True)
def test_function():
pass
复制代码
[pytest]
xfail_strict=true
复制代码
6.関連するコード例
import pytest
xfail = pytest.mark.xfail
@xfail
def test_hello():
assert 0
@xfail(run=False)
def test_hello2():
assert 0
@xfail("hasattr(os, 'sep')")
def test_hello3():
assert 0
@xfail(reason="bug 110")
def test_hello4():
assert 0
@xfail('pytest.__version__[0] != "17"')
def test_hello5():
assert 0
def test_hello6():
pytest.xfail("reason")
@xfail(raises=IndexError)
def test_hello7():
x = []
x[1] = 1
复制代码
C:\Users\mc\Desktop\python基础>pytest -rx test_module.py
======================================================================= test session starts ========================================================================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1
rootdir: C:\Users\mc\Desktop\python基础
collected 7 items
test_module.py xxxxxxx [100%]
===================================================================== short test summary info ======================================================================
XFAIL test_module.py::test_hello
XFAIL test_module.py::test_hello2
reason: [NOTRUN]
XFAIL test_module.py::test_hello3
condition: hasattr(os, 'sep')
XFAIL test_module.py::test_hello4
bug 110
XFAIL test_module.py::test_hello5
condition: pytest.__version__[0] != "17"
XFAIL test_module.py::test_hello6
reason: reason
XFAIL test_module.py::test_hello7
======================================================================== 7 xfailed in 0.19s ========================================================================
复制代码
2.13.3パラメーター化されたスキップ/終了の使用
import sys
import pytest
@pytest.mark.parametrize(
("n", "expected"),
[
(1, 2),
pytest.param(1, 0, marks=pytest.mark.xfail),
pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
(2, 3),
(3, 4),
(4, 5),
pytest.param(10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k˓→")),
],
)
def test_increment(n, expected):
assert n + 1 == expected
复制代码
C:\Users\mc\Desktop\python>pytest -vs test_module.py
======================================================================= test session starts ========================================================================
platform win32 -- Python 3.9.6, pytest-7.1.1, pluggy-0.13.1 -- c:\python39\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\mc\Desktop\python
collected 7 items
test_module.py::test_increment[1-2] PASSED
test_module.py::test_increment[1-0] XFAIL
test_module.py::test_increment[1-3] XFAIL (some bug)
test_module.py::test_increment[2-3] PASSED
test_module.py::test_increment[3-4] PASSED
test_module.py::test_increment[4-5] PASSED
test_module.py::test_increment[10-11] SKIPPED (py2k˓→)
============================================================= 4 passed, 1 skipped, 2 xfailed in 0.12s ==============================================================
复制代码