前言
在实际工作中,经常需要跳过某个测试用例,比如现阶段某个功能还没有开发完毕,但是先把测试用例写到了pytest中,因此需要先把这个测试用例给跳过。 可以使用如下方式跳过用例:
1、无条件跳过该用例
使用@pytest.mark.skip(self,reason=None):在要跳过的测试用例前加入该标签,并可以选择传入一个非必须参数reason表示原因 代码如下:
#coding=utf-8
import time
import pytest
class Test_Pytest():
@pytest.mark.skip(reason='无条件跳过') # 无条件跳过
def test_one(self):
print("test_one方法执行" )
def test_two(self):
print("test_two方法执行" )
def test_three(self):
print("test_three方法执行" )
def test_four(self):
print("test_four方法执行")
运行结果如下
2、有条件跳过该用例
使用@pytest.mark.skipif(self,condition,reason=None):在要跳过的测试用例前加入该标签,根据condition条件判断是否进行跳过 代码如下
#coding=utf-8
import time
import pytest
class Test_Pytest():
@pytest.mark.skipif(1<3,reason='条件成立时跳过') # 无条件跳过
def test_one(self):
print("test_one方法执行" )
def test_two(self):
print("test_two方法执行" )
def test_three(self):
print("test_three方法执行" )
def test_four(self):
print("test_four方法执行")
运行结果 条件成立所以跳过
3、用例里面添加判断是否跳过
使用skip()方法:在测试用例中调用pytest.skip()方法来实现跳过,可以选择传入msg参数来说明跳过原因;如果想要通过判断是否跳过,可以写在if判断里
#coding=utf-8
import time
import pytest
class Test_Pytest():
def test_one(self):
if True:
pytest.skip('方法内部跳过')
print("test_one方法执行" )
def test_two(self):
print("test_two方法执行" )
def test_three(self):
print("test_three方法执行" )
def test_four(self):
print("test_four方法执行")
结果如下图所示
4、跳过整个模块
使用pytestmark=pytest.mark.skip()方法:可以跳过整个模块,注意pytestmark为关键字,必须用此名称 代码如下
#coding=utf-8
import time
import pytest
pytestmark=pytest.mark.skip('跳过整个模块')
class Test_Pytest():
def test_one(self):
if True:
pytest.skip('方法内部跳过')
print("test_one方法执行" )
def test_two(self):
print("test_two方法执行" )
def test_three(self):
print("test_three方法执行" )
def test_four(self):
print("test_four方法执行")
运行结果