前言
日常工作中,我们难免会遇到本次执行不需要所有用例都跑一遍的情况,或者说,我们就是希望某些用例不执行,来看看报错。
那除了我们手动去注释掉部分用例,还有没有其他的办法自动地跳过部分用例呢?
Pytest很懂我们,真的很懂我们,给我们提供了skip方法,可以帮助我们实现需求。
skip用法介绍
- pytest.main([‘-vs’,’test01.py’]) 用-vs执行,跳过原因才会显示SKIPPED [1] test01.py:415: 跳过Test类,会跳过类中所有方法
- skip跳过,无条件和原因@pytest.mark.skipif()
- skip跳过,无需满足条件true、有跳过原因@pytest.mark.skipif(reason=’无条件,只有跳过原因’)
- skip跳过,需满足条件true、且有跳过原因@pytest.mark.skipif(条件1==1,reason=’跳过原因’)
- skip赋值变量,多处调用myskip=pytest.mark.skipif(1==1,reason=’skip赋值给变量,可多处调用’)
然后@myskip使用
跳过测试类
@pytest.mark.skip()和@pytest.mark.skipif()两个标签,用他们装饰测试类
- @pytest.mark.skip()被标记的类中所有方法测试用例都会被跳过
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 ==============================
- 被标记的类,当条件为ture时,会被跳过,否则不跳过
被标记的类,当条件成立时,会跳过类中的所有方法
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种都可以
- 跳过方法或用例,未备注原因 @pytest.mark.skip()
- 跳过方法或用例,备注原因 @pytest.mark.skip(reason=’跳过一个方法或一个测试用例’)
- 当条件满足,跳过方法或用例,备注原因 @pytest.mark.skipif(1==1,reason=’跳过一个方法或一个测试用例’)
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 ==============================
多个skip时,满足1个条件即跳过
- 如果类中的条件满足,无论方法中的条件是否满足,均跳过该类下的所有方法,如下所示:
import pytest
@pytest.mark.skipif(1 == 1, reason='当类中条件满足,会跳过类中的所有方法')
class TestClass(object):
@pytest.mark.skip(reason='跳过不执行')
def test1(self):
print('test1')
# 类中条件不满足,方法中条件满足,跳过不执行
@pytest.mark.skipif(1 == 1, reason='条件满足,跳过不执行')
def test2(self):
print('test2')
# 类中条件不满足,方法中条件也不满足,不跳过继续执行
@pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过,继续执行')
def test3(self):
print('test3')
if __name__ == '__main__':
pytest.main(['-vs', 'test_demo.py'])
------------------------------------------
============================= test session starts =============================
collecting ... collected 3 items
test_demo.py::TestClass::test1 SKIPPED (当类中条件满足,会跳过类中的所有方法) [ 33%]
Skipped: 当类中条件满足,会跳过类中的所有方法
test_demo.py::TestClass::test2 SKIPPED (条件满足,跳过不执行) [ 66%]
Skipped: 条件满足,跳过不执行
test_demo.py::TestClass::test3 SKIPPED (当类中条件满足,会跳过类中的所有方法) [100%]
Skipped: 当类中条件满足,会跳过类中的所有方法
============================= 3 skipped in 0.02s ==============================
- 如果类中不满足条件,方法中满足条件,跳过方法;如果类中不满足条件,方法中也不满足条件,继续执行方法中的代码;如下所示:
import pytest
@pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过') # 类中不满足条件
class TestClass(object):
@pytest.mark.skip(reason='跳过不执行')
def test1(self):
print('test1')
# 类中条件不满足,方法中条件满足,跳过不执行
@pytest.mark.skipif(1 == 1, reason='条件满足,跳过不执行')
def test2(self):
print('test2')
# 类中条件不满足,方法中条件也不满足,不跳过继续执行
@pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过,继续执行')
def test3(self):
print('test3')
if __name__ == '__main__':
pytest.main(['-vs', 'test_demo.py'])
------------------------------------
============================= test session starts =============================
collecting ... collected 3 items
test_demo.py::TestClass::test1 SKIPPED (跳过不执行) [ 33%]
Skipped: 跳过不执行
test_demo.py::TestClass::test2 SKIPPED (条件满足,跳过不执行) [ 66%]
Skipped: 条件满足,跳过不执行
test_demo.py::TestClass::test3 PASSED [100%]test3
======================== 1 passed, 2 skipped in 0.02s =========================
pytest.skip()方法内跳过
除了通过使用标签的方式,还可以在测试用例中调用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的特性,提升我们的工作效率!