如何跳过测试用例:
skip 直接跳过测试用例:
使用方法:@pytest.mark.skip(reason='这个是跳过的原因!') 装饰要跳过的测试方法或测试类上;
- 装饰在测试方法只跳过该方法对应的测试用例;
- 装饰在使用了 parametrize 参数化的测试方法上,跳过生成的所有测试用例;
- 装饰在测试类上,跳过测试类中所有的测试用例;
- reason 参数是跳过用例的原因,可以不写;
# test_moduleName.py
import pytest
class TestClass001:
def test_func_001(self):
pass
@pytest.mark.skip(reason="这个是跳过的原因!") # 装饰在单个的测试方法上,只跳过1个测试用例
def test_func_002(self):
pass
@pytest.mark.skip # 装饰在使用了 parametrize 参数化的测试方法上,跳过生成的所有测试用例
@pytest.mark.parametrize('data', [1, 2, 3])
def test_func_003(self, data):
pass
@pytest.mark.skip # 装饰在测试类上,跳过测试类中所有的测试用例
class TestClass002:
def test_func_004(self):
pass
def test_func_005(self):
pass
# 执行结果
============================= test session starts =============================
collected 7 items
test_moduleName.py::TestClass001::test_func_001 PASSED
test_moduleName.py::TestClass001::test_func_002 SKIPPED
test_moduleName.py::TestClass001::test_func_003[1] SKIPPED
test_moduleName.py::TestClass001::test_func_003[2] SKIPPED
test_moduleName.py::TestClass001::test_func_003[3] SKIPPED
test_moduleName.py::TestClass002::test_func_004 SKIPPED
test_moduleName.py::TestClass002::test_func_005 SKIPPED
======================== 1 passed, 6 skipped in 0.22s =========================
Process finished with exit code 0
skipif 满足某些条件时跳过测试用例:
使用方法:@pytest.mark.skipif(condition, reason='跳过的原因!') 装饰在要跳过的测试方法或测试类上;
- condition 参数:condition 是条件表达式,如果 condition 为 Ture,则跳过测试用例,否则便不跳过;
- condition 参数默认为 True,和 reason 参数一样可以省略不写;
- condition 条件表达式必须是字符串(条件表达式外面加上引号便可,pytest 会自动解析);
# test_moduleName.py
import pytest
# 校验条件
device_sys = 'Android'
android_sys = 6.0
class TestClass001:
def test_func_001(self):
pass
# 条件表达式不成立,此用例不会被跳过
@pytest.mark.skipif(condition='android_sys < 6.0', reason='系统版本过低,此用例跳过!')
def test_func_002(self):
pass
# condition 参数默认为 True,和 reason 参数一样可以省略不写;
@pytest.mark.skipif
def test_func_003(self, data):
pass
# 条件表达式成立,此测试类中所有用例被跳过
@pytest.mark.skipif('device_sys != "iOS"', reason='iOS设备专用case,Android设备需跳过!')
class TestClass002:
def test_func_004(self):
pass
def test_func_005(self):
pass
# 执行结果
============================= test session starts =============================
collected 5 items
test_moduleName.py::TestClass001::test_func_001 PASSED
test_moduleName.py::TestClass001::test_func_002 PASSED
test_moduleName.py::TestClass001::test_func_003 SKIPPED
test_moduleName.py::TestClass002::test_func_004 SKIPPED
test_moduleName.py::TestClass002::test_func_005 SKIPPED
======================== 2 passed, 3 skipped in 0.05s =========================
使用 pytest.skip() 在用例内部跳过决定是否跳过测试用例:
当用例是否跳过的判断条件在导入时无法判断时,可以在用例内部执行时再进行跳过:pytest.skip(reason='跳过的原因!')
注意:pytest.skip(reason='跳过的原因!') 中间没有 mark;
# test_moduleName.py
android_sys = 5.0
class TestClass001:
def test_func_001(self):
pass
def test_func_002(self):
if android_sys < 6:
pytest.skip('系统版本过低,此用例不适用!')
# 执行结果
============================= test session starts =============================
collected 2 items
test_moduleName.py::TestClass001::test_func_001 PASSED
test_moduleName.py::TestClass001::test_func_002 SKIPPED
======================== 1 passed, 1 skipped in 0.14s =========================
使用 pytest.skip() 跳过整个模块:
import pytest
import sys
if sys.platform.startswith("win"): # 如果平台不是windows
# 跳过整个模块
pytest.skip(msg='MacOS 专用case, Windows 系统需跳过!', allow_module_level=True)
全局共享 skip 标记:
- 在公共模块创建 skip 标记;
- 在测试模块导入 skip 标记;
- 装饰在测试类或测试方法上;
# conftest.py
import pytest
# 校验条件
version = 5
# 1. 创建 skip 标记
isSkip = pytest.mark.skipif(version < 6, reason="版本过低,此用例不适用!")
# test_moduleName.py
2. 导入 skip 标记
from .conftest import isSkip
class TestClass001:
# 用 skip 标记装饰测试方法或者测试类
@isSkip
def test_func_001(self):
pass
def test_func_002(self):
pass
导入模块失败时跳过测试用例:
importorskip(modname, minversion=None, reason=None);
注意:如果模块导入成功,该方法会返回模块本身,如果导入失败,则会跳过测试模块后面的所有代码;
- modname 参数 :要导入的模块名称;
- minversion 参数 :要导入模块的最低版本号, 如果给定,导入的模块__version__属性必须至少为此最小版本,否则仍会跳过测试;
- reason 参数 :跳过理由;
# test_moduleName.py
import pytest
# 如果模块导入成功,该方法会返回模块本身,如果导入失败,则会跳过测试模块后面的所有代码;
time = pytest.importorskip('time')
time.sleep(3)
class TestClass001:
def test_func_001(self):
pass
def test_func_002(self):
pass
处理预计会失败的用例:
@pytest.mark.xfail(condition, reason='理由!')
- 预计失败,condition条件满足/表达式为True时,用例失败则标记为 XFAIL,用例通过则标记为 XPASS,表达式为False时没任何作用!
- 通过修改 pytest.ini 可以强制执行 XPASS 为 Fail
[pytest]
xfail_strict = true
# test_moduleName.py
import pytest
# 校验条件
sys_version = 8
class TestClass001:
# 表达式不成立,此方法不生效
@pytest.mark.xfail(sys_version < 5, reason='原因!')
def test_func_001(self):
pass
# 表达式成立,预计失败,实际通过,结果为 XPASS
@pytest.mark.xfail(sys_version < 10, reason='原因!')
def test_func_002(self):
assert 1
# 表达式成立,预计失败,实际失败,结果为 XFAIL
@pytest.mark.xfail(sys_version < 10, reason='原因!')
def test_func_003(self):
assert 0
# 执行结果
============================= test session starts =============================
collected 3 items
test_moduleName.py::TestClass001::test_func_001 PASSED
test_moduleName.py::TestClass001::test_func_002 XPASS
test_moduleName.py::TestClass001::test_func_003 XFAIL
=================== 1 passed, 1 xfailed, 1 xpassed in 0.05s ===================