pytest.mark.skip()
@pytest.mark.skip([reason=""])reason为可选参数
【注】:@pytest.mark.skip可以加载函数用例上,类方法上,类上。
#coding=utf-8
import pytest
@pytest.fixture(autouse=True)
def login():
print("登录")
def test_01():
print("第一个测试用例test_01")
@pytest.mark.skip(reason="因为没写好")
def test_02():
print("第二个测试用例test_02")
class Test1:
def test_case1(self):
print("第一个类测试用例test_case1")
@pytest.mark.skip(reason="跳过类测试用例")
def test_case2(self):
print("第二个类测试用例test_case2")
@pytest.mark.skip(reason="类也可以跳过不执行")
class TestSkip:
def test_skip(self):
print("第三个类测试用例test_skip")
if __name__ == '__main__':
pytest.main(["-sv","test_skip.py"])
"""
结果:
test_skip.py::test_01 登录
第一个测试用例test_01
PASSED
test_skip.py::test_02 SKIPPED (因为没写好)
test_skip.py::Test1::test_case1 登录
第一个类测试用例test_case1
PASSED
test_skip.py::Test1::test_case2 SKIPPED (跳过类测试用例)
test_skip.py::TestSkip::test_skip SKIPPED (类也可以跳过不执行)
"""
pytest.skip()
pytest.skip()
作用:
在测试用例执行期间强制跳过不再执行剩余内容。
类似,在循环里遇到break退出循环。
#coding=utf-8
import pytest
def test_run():
n=0
while True:
n=n+1
print("这是第{}次执行用例".format(n))
if n == 5:
pytest.skip("执行次数够了,不再执行")
if __name__ == '__main__':
pytest.main(["-sv","test_skip2.py"])
"""
结果:
test_skip2.py::test_run 这是第1次执行用例
这是第2次执行用例
这是第3次执行用例
这是第4次执行用例
这是第5次执行用例
SKIPPED (执行次数够了,不再执行)
"""
pytest.skip(msg="",allow_module_level=False)
allow_module_level=True时,可以设置模块级别跳过整个模块不执行
#coding=utf-8
import pytest
import sys
if sys.platform.startswith("win"):
pytest.skip(msg="skipping windows testcase",allow_module_level=True)
class TestCase:
@pytest.fixture(autouse=True)
def login(self):
print("登录")
def test_01(self):
print("执行测试用例1")
if __name__ == '__main__':
pytest.main(["-sv","test_skip3.py"])
"""
结果:
Traceback (most recent call last):
File "E:/ProjectStudy/Pytest框架/test_skip3.py", line 7, in <module>
pytest.skip("skipping windows testcase",allow_module_level=True)
File "G:\Python38\lib\site-packages\_pytest\outcomes.py", line 139, in skip
raise Skipped(msg=msg, allow_module_level=allow_module_level)
Skipped: skipping windows testcase
"""
pytest.mark.skipif
@pytest.mark.skipif(condition="",reason="")
作用:希望有条件的跳过某些测试用例。
注:condition返回True才会跳过用例
#coding=utf-8
import sys
import pytest
@pytest.mark.skipif(sys.platform=="win32",reason="does not run on windows")
class TestSkipif:
def test_01(self):
print("不能在windows上运行")
if __name__ == '__main__':
pytest.main(["-sv","test_skip4.py"])
"""
结果:
test_skip4.py::TestSkipif::test_01 SKIPPED (does not run on windows)
"""
可以将 pytest.mark.skip 和 pytest.mark.skipif 赋值给一个标记变量,
在不同模块之间共享这个标记变量。
若有多个模块的测试用例需要用到相同的 skip 或 skipif ,
可以用一个单独的文件去管理这些通用标记,然后适用于整个测试用例集。
#coding=utf-8
import sys
import pytest
skipmark=pytest.mark.skip(reason="不能在windows上运行")
skipifmark=pytest.mark.skipif(sys.platform=="win32",reason="不能在windows上运行")
class TestMark:
def test_01(self):
print("test_01")
@skipmark
def test_02(self):
print("test_02")
@skipifmark
def test_03():
print("test_03")
if __name__ == '__main__':
pytest.main(["-sv","test_skip5.py"])
"""
结果:
test_skip5.py::TestMark::test_01 test_01
PASSED
test_skip5.py::TestMark::test_02 SKIPPED (不能在windows上运行)
test_skip5.py::test_03 SKIPPED (不能在windows上运行)
"""
pytest.importorskip
pytest.importorskip(modename,minversion,reason)
作用:如果缺少某些导入的包,则跳过模块中的所有测试
参数:
modname:模块名
minversion:版本号
reason:跳过原因,可选参数
#coding=utf-8
import pytest
pexpect=pytest.importorskip(modname="pexpect",minversion="0.3")
@pexpect
def test_01():
print("测试用例test_01")
if __name__ == '__main__':
pytest.main(["-sv","test_skip6.py"])
"""
结果:
Traceback (most recent call last):
File "E:/ProjectStudy/Pytest框架/test_skip6.py", line 4, in <module>
pexpect=pytest.importorskip(modname="pexpect",minversion="0.3")
File "G:\Python38\lib\site-packages\_pytest\outcomes.py", line 212, in importorskip
raise Skipped(reason, allow_module_level=True) from None
Skipped: could not import 'pexpect': No module named 'pexpect'
"""
网友评论