Time will tell.
一、skip 介绍
在自动化测试过程中经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致一些用例执行不了,这时就可以用到跳过skip
用例,如果我们注释掉或删除掉,后面还要进行恢复操作。
1、skip
跳过成功,标识为:
============================= 2 skipped in 0.04s ==============================
2、pytest.main(['-rs','test01.py'])
用 -rs 执行,跳过原因才会显示SKIPPED [1] test01.py:415:
跳过 Test 类,会跳过类中所有方法。
3、skip跳过,无条件和原因@pytest.mark.skip()
。
4、skip跳过,无需满足条件true
、有跳过原因@pytest.mark.skipif(reason=‘无条件,只有跳过原因’)。
5、skip跳过,需满足条件true
、且有跳过原因@pytest.mark.skipif
(条件1==1,reason=‘跳过原因’)。
6、skip赋值变量,多处调用myskip=pytest.mark.skipif
(1==1,reason=‘skip赋值给变量,可多处调用’)。
然后@myskip
使用。
二、跳过测试类
@pytest.mark.skip()
和@pytest.mark.skipif()
两个标签,用它们装饰测试类:
-
被标记的类中所有方法测试用例都会被跳过。
-
被标记的类,当条件为ture时,会被跳过,否则不跳过。
'''
skip跳过类
'''
import pytest,sys
@pytest.mark.skip(reason='跳过Test类,会跳过类中所有方法')
class Test(object):
def test_one(self):
assert 1==1
def test_two(self):
print('test_02')
assert 1==2
if __name__=='__main__':
pytest.main(['-rs','test01.py'])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
test01.py ss [100%]
=========================== short test summary info ===========================
SKIPPED [2] test01.py: 跳过Test类,会跳过类中所有方法
============================= 2 skipped in 0.07s ==============================
Process finished with exit code 0
#skip满足条件,skip跳过类
import pytest,sys
@pytest.mark.skipif(1==1,reason='跳过Test类,会跳过类中所有方法')
class Test(object):
def test_one(self):
assert 1==1
def test_two(self):
print('test_02')
assert 1==2
if __name__=='__main__':
pytest.main(['-rs','test01.py'])
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts ==========