为了避免由于环境发生改变或者其他原因造成的测试脚本的阻塞,可用skip跳过某些用例。
1.@pytest.mark.skip()的功能是跳过,可装饰于类与方法。括号内(reason=‘跳过原因’)
2.@pytest.mark.skipif()的功能是跳过,可装饰于类与方法。括号内(判断, reason=‘跳过原因’)
3.pytest.skip()也是跳过,不同于1的是它一般用于方法内,当进行if条件判断后执行该语句。1则更类似于一个烙印,打上后直接跳过。
import pytest
class Test1(object):
def test_1(self):
print('1')
class Test2(object):
@pytest.mark.skip(reason='跳过Test2的test_2')
def test_2(self):
print('2')
def test_3(self):
print('3')
@pytest.mark.skip(reason='跳过Test3')
class Test3(object):
def test_4(self):
print('4')
def test_5(self):
print('5')
def reason():
r = 1
return r
class Test4(object):
@pytest.mark.skipif(reason()==1, reason='跳过test_6')
def test_6(self):
print('6')
def test_7(self):
print('7')
def test_8(self):
if True:
pytest.skip('跳过test_8')
else:
print('8')
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- E:\pythonPJ\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\pythonPJ\ruhr_cloud, configfile: pytest.ini
plugins: allure-pytest-2.8.40
collecting ... collected 8 items
展示板.py::Test1::test_1
展示板.py::Test2::test_2
展示板.py::Test2::test_3
展示板.py::Test3::test_4
展示板.py::Test3::test_5
展示板.py::Test4::test_6 PASSED [ 12%]1
SKIPPED (跳过Test2的test_2) [ 25%]
Skipped: 跳过Test2的test_2
PASSED [ 37%]3
SKIPPED (跳过Test3) [ 50%]
Skipped: 跳过Test3
SKIPPED (跳过Test3) [ 62%]
Skipped: 跳过Test3
SKIPPED (跳过test_6) [ 75%]
Skipped: 跳过test_6
展示板.py::Test4::test_7 PASSED [ 87%]7
展示板.py::Test4::test_8 SKIPPED (跳过test_8) [100%]
Skipped: 跳过test_8
======================== 3 passed, 5 skipped in 0.04s =========================