章节目录:
- 一、概述
- 二、跳过用例函数
- 三、执行期间跳过剩余步骤
- 四、跳过整个测试模块
- 五、判断跳过部分用例
- 六、跳过标记
- 七、依赖检查
- 八、结束语
一、概述
@pytest.mark.skip
可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能。- 希望满足某些条件才执行某些测试用例,否则 pytest 会跳过运行该测试用例。
- 实际常见场景:跳过非Windows平台上的仅Windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试。
二、跳过用例函数
使用
@pytest.mark.skip
。
- 代码示例:
import pytestdef test_case01():print("test_case01 run")@pytest.mark.skip(reason="用例存在问题!")
def test_case02():print("test_case02 run")@pytest.mark.skip(reason="用例未编写完成!")
def test_case03():print("test_case03 run")@pytest.mark.skip("该类的所有用例都跳过!")
class TestSample:def test_case04(self):print("test_case04 run")# test_case01 run
# test_case02 SKIPPED (用例存在问题!)
# test_case03 SKIPPED (用例未编写完成!)
# test_case04 SKIPPED (该类的所有用例都跳过!)
# ======================== 1 passed, 3 skipped in 0.01s =========================
@pytest.mark.skip
可以加在函数或者类上。- 加在类上时,该类下的所有测试用例都不会执行。
三、执行期间跳过剩余步骤
使用
pytest.skip()
函数。
- 代码示例:
import pytestdef test_case01():c = 0while True:print(f"run {c} 次。")if c == 3:pytest.skip("3次后,停止执行。")c += 1# run 1 次。
# run 2 次。
# run 3 次。
# Skipped: 3次后,停止执行。
# ============================= 1 skipped in 0.01s ==============================
四、跳过整个测试模块
使用
pytest.skip(msg="",allow_module_level=True)
。
- 代码示例:
import sysimport pytestif sys.platform.startswith("win"):pytest.skip("跳过仅在 windows 上运行的测试用例集。", allow_module_level=True)@pytest.fixture(autouse=True)
def login():print("login")def test_case01():print("test_case01")# Skipped: 跳过仅在 windows 上运行的测试用例集。
# ============================= 1 skipped in 0.01s ==============================
五、判断跳过部分用例
使用
@pytest.mark.skipif(condition, reason="")
。condition 需要返回True
才会跳过。
- 代码示例:
import sysimport pytestenvironment = "android"@pytest.mark.skipif("environment=='android'", reason="android 平台没有这个功能。")
def test_case_01():print("test_case_01")@pytest.mark.skipif(sys.platform == "linux", reason="不在 linux 下运行。")
@pytest.mark.skipif(sys.version_info < (3, 6), reason="Python 3.6 版本以下不执行,你需要更高版本。")
def test_case_02():print("test_case_02")# test_case_01 SKIPPED (android 平台没有这个功能。)
# test_case_02 PASSED
# ======================== 1 passed, 1 skipped in 0.01s =========================
六、跳过标记
被赋值的
@skipmark
和@skipifmark
为通用跳过标记,可添加到需要用到的测试用例上。
- 代码示例:
import sysimport pytest# 标记设置。
skipmark = pytest.mark.skip(reason="标记跳过!")
skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在 windows 上运行!")@skipmark
def test_case01():print("test_case01")@skipifmark
def test_case02():print("test_case02")# test_case01 SKIPPED (标记跳过!)
# test_case02 SKIPPED (不能在 windows 上运行!)
# ============================= 2 skipped in 0.01s ==============================
七、依赖检查
如果缺少某些导入,则跳过模块中的所有测试:
pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )
- 代码示例:
import pytest# "none" 为不存在的模块名,minversion 则是进行模块版本检查。
pexpect = pytest.importorskip("none", minversion="0.3")@pexpect
def test_import():print("test_import")# Skipped: could not import 'none': No module named 'none'
八、结束语
“-------怕什么真理无穷,进一寸有一寸的欢喜。”
微信公众号搜索:饺子泡牛奶。