日常工作中,我们难免会遇到本次执行不需要所有用例都跑一遍的情况,或者说,我们就是希望某些用例不执行,来看看报错。
那除了我们手动去注释掉部分用例,还有没有其他的办法自动地跳过部分用例呢?
Pytest很懂我们,真的很懂我们,给我们提供了skip方法,可以帮助我们实现需求。
@pytest.mark.skip()和@pytest.mark.skipif()两个标签,用他们装饰测试类
import pytest
@pytest.mark.skip(reason='跳过TestSkip类中的所有方法')
class TestSkip(object):
def test01(self):
print('test01')
assert 1 == 1
def test02(self):
print('test02')
assert 1 == 1
if __name__ == '__main__':
pytest.main(['-vs', 'test_skip.py'])
-----------------------
test_skip.py::TestSkip::test01 SKIPPED (跳过TestSkip类中的所有方法)
Skipped: 跳过TestSkip类中的所有方法
test_skip.py::TestSkip::test02 SKIPPED (跳过TestSkip类中的所有方法)
Skipped: 跳过TestSkip类中的所有方法
============================= 2 skipped in 0.04s ==============================
import pytest
@pytest.mark.skipif(1 == 1, reason='当条件成立,跳过类中的所有方法')
class TestSkipif(object):
def test03(self):
print('test03')
assert 3 == 3
def test04(self):
print('test04')
assert 4 == 4
if __name__ == '__main__':
pytest.main(['-vs', 'test_skip.py'])
--------------------------
test_skip.py::TestSkipif::test03 SKIPPED (当条件成立,跳过类中的所有方法)
Skipped: 当条件成立,跳过类中的所有方法
test_skip.py::TestSkipif::test04 SKIPPED (当条件成立,跳过类中的所有方法)
Skipped: 当条件成立,跳过类中的所有方法
============================= 2 skipped in 0.04s ==============================
被标记的类,当条件不成立时,不会跳过类中的所有方法
import pytest
@pytest.mark.skipif(1 == 3, reason='当条件不成立,不跳过类中的所有方法')
class TestSkipif(object):
def test03(self):
print('test03')
assert 3 == 3
def test04(self):
print('test04')
assert 4 == 4
if __name__ == '__main__':
pytest.main(['-vs', 'test_skip.py'])
-----------------------
test_skip.py::TestSkipif::test03 test03
PASSED
test_skip.py::TestSkipif::test04 test04
PASSED
============================== 2 passed in 0.04s ==============================
我们想要某个方法或跳过某条用例,在方法上加以下3种都可以
import pytest
class TestClass(object):
# 跳过方法,未备注原因
@pytest.mark.skip()
def test_one(self):
print('test_one')
# 跳过方法,并备注原因
@pytest.mark.skip(reason='跳过有原因')
def test_two(self):
print('test_two')
# 当条件满足时,跳过方法,并备注原因
@pytest.mark.skipif(1 == 1, reason='条件成立,跳过有原因')
def test_three(self):
print('test_three')
if __name__ == '__main__':
pytest.main(['-vs', 'test_skip.py'])
----------------------------------------
test_skip.py::TestClass::test_one SKIPPED (unconditional skip)
Skipped: unconditional skip
test_skip.py::TestClass::test_two SKIPPED (跳过有原因)
Skipped: 跳过有原因
test_skip.py::TestClass::test_three SKIPPED (条件成立,跳过有原因)
Skipped: 条件成立,跳过有原因
============================= 3 skipped in 0.04s ==============================
除了通过使用标签的方式,还可以在测试用例中调用pytest.skip()方法来实现跳过,可以选择传入reason参数来说明跳过原因;如果想要通过判断是否跳过,可以写在if判断里(_)
import pytest
class TestClass(object):
def test001(self):
if 'h' in 'hell':
pytest.skip(reason='跳过,不执行') # 不执行后面的代码
print('test001')
def test002(self):
print('test002')
if __name__ == '__main__':
pytest.main(['-vs', 'test_demo.py'])
============================= test session starts =============================
collecting ... collected 2 items
test_demo.py::TestClass::test001 SKIPPED (跳过,不执行) [ 50%]
Skipped: 跳过,不执行
test_demo.py::TestClass::test002 PASSED [100%]test002
======================== 1 passed, 1 skipped in 0.02s =========================
关于pytest跳过用例执行的介绍就到这里,我们可以灵活使用skip的特性,提升我们的工作效率!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。