pytest-skip的使用总结
作者:互联网
一、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.skipif()
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()两个标签,用他们装饰测试类
1、@pytest.mark.skip()被标记的类中所有方法测试用例都会被跳过
1 import pytest 2 3 4 @pytest.mark.skip(reason='跳过TestSkip类中的所有方法') 5 class TestSkip(object): 6 def test01(self): 7 print('test01') 8 assert 1 == 1 9 10 def test02(self): 11 print('test02') 12 assert 1 == 1 13 14 15 if __name__ == '__main__': 16 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestSkip::test01 SKIPPED (跳过TestSkip类中的所有方法) 2 Skipped: 跳过TestSkip类中的所有方法 3 4 test_skip.py::TestSkip::test02 SKIPPED (跳过TestSkip类中的所有方法) 5 Skipped: 跳过TestSkip类中的所有方法 6 7 8 ============================= 2 skipped in 0.04s ==============================
2、被标记的类,当条件为ture时,会被跳过,否则不跳过
2.1 被标记的类,当条件成立时,会跳过类中的所有方法
1 import pytest 2 3 @pytest.mark.skipif(1 == 1, reason='当条件成立,跳过类中的所有方法') 4 class TestSkipif(object): 5 def test03(self): 6 print('test03') 7 assert 3 == 3 8 9 def test04(self): 10 print('test04') 11 assert 4 == 4 12 13 if __name__ == '__main__': 14 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestSkipif::test03 SKIPPED (当条件成立,跳过类中的所有方法) 2 Skipped: 当条件成立,跳过类中的所有方法 3 4 test_skip.py::TestSkipif::test04 SKIPPED (当条件成立,跳过类中的所有方法) 5 Skipped: 当条件成立,跳过类中的所有方法 6 7 ============================= 2 skipped in 0.04s ==============================
2.2 被标记的类,当条件不成立时,不会跳过类中的所有方法
1 import pytest 2 3 @pytest.mark.skipif(1 == 3, reason='当条件不成立,不跳过类中的所有方法') 4 class TestSkipif(object): 5 def test03(self): 6 print('test03') 7 assert 3 == 3 8 9 def test04(self): 10 print('test04') 11 assert 4 == 4 12 13 if __name__ == '__main__': 14 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestSkipif::test03 test03 2 PASSED 3 test_skip.py::TestSkipif::test04 test04 4 PASSED 5 6 ============================== 2 passed in 0.04s ==============================
三、跳过方法或测试用例
我们想要某个方法或跳过某条用例,在方法上加以下3种都可以
@pytest.mark.skip() #1、跳过方法或用例,未备注原因
@pytest.mark.skip(reason='跳过一个方法或一个测试用例') #2、跳过方法或用例,备注了原因
@pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例') #3、当条件满足,跳过方法或用例,备注了原因
1 import pytest 2 3 class TestClass(object): 4 # 跳过方法,未备注原因 5 @pytest.mark.skip() 6 def test_one(self): 7 print('test_one') 8 9 # 跳过方法,并备注原因 10 @pytest.mark.skip(reason='跳过有原因') 11 def test_two(self): 12 print('test_two') 13 14 # 当条件满足时,跳过方法,并备注原因 15 @pytest.mark.skipif(1 == 1, reason='条件成立,跳过有原因') 16 def test_three(self): 17 print('test_three') 18 19 20 if __name__ == '__main__': 21 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestClass::test_one SKIPPED (unconditional skip) 2 Skipped: unconditional skip 3 4 test_skip.py::TestClass::test_two SKIPPED (跳过有原因) 5 Skipped: 跳过有原因 6 7 test_skip.py::TestClass::test_three SKIPPED (条件成立,跳过有原因) 8 Skipped: 条件成立,跳过有原因 9 10 11 ============================= 3 skipped in 0.04s ==============================
四、多个skip时,满足1个条件即跳过(我们在类和方法上分别加了skip)
1.如果类中的条件满足,无论方法中的条件是否满足,均跳过该类下的所有方法,如下所示:
1 import pytest 2 @pytest.mark.skipif(1 == 1, reason='当类中条件满足,会跳过类中的所有方法') 3 class TestClass(object): 4 @pytest.mark.skip(reason='跳过不执行') 5 def test1(self): 6 print('test1') 7 8 # 类中条件不满足,方法中条件满足,跳过不执行 9 @pytest.mark.skipif(1 == 1, reason='条件满足,跳过不执行') 10 def test2(self): 11 print('test2') 12 13 # 类中条件不满足,方法中条件也不满足,不跳过继续执行 14 @pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过,继续执行') 15 def test3(self): 16 print('test3') 17 18 if __name__ == '__main__': 19 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestClass::test1 SKIPPED (条件不满足,不跳过) 2 Skipped: 条件不满足,不跳过 3 4 test_skip.py::TestClass::test2 SKIPPED (条件满足,跳过不执行) 5 Skipped: 条件满足,跳过不执行 6 7 test_skip.py::TestClass::test3 SKIPPED (条件不满足,不跳过) 8 Skipped: 条件不满足,不跳过 9 10 11 ============================= 3 skipped in 0.04s ==============================
2.如果类中不满足条件,方法中满足条件,跳过方法;如果类中不满足条件,方法中也不满足条件,继续执行方法中的代码;如下所示:
1 import pytest 2 3 @pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过') # 类中不满足条件 4 class TestClass(object): 5 @pytest.mark.skip(reason='跳过不执行') 6 def test1(self): 7 print('test1') 8 9 # 类中条件不满足,方法中条件满足,跳过不执行 10 @pytest.mark.skipif(1 == 1, reason='条件满足,跳过不执行') 11 def test2(self): 12 print('test2') 13 14 # 类中条件不满足,方法中条件也不满足,不跳过继续执行 15 @pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过,继续执行') 16 def test3(self): 17 print('test3') 18 19 20 if __name__ == '__main__': 21 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestClass::test1 SKIPPED (跳过不执行) 2 Skipped: 跳过不执行 3 4 test_skip.py::TestClass::test2 SKIPPED (条件满足,跳过不执行) 5 Skipped: 条件满足,跳过不执行 6 7 test_skip.py::TestClass::test3 test3 8 PASSED 9 10 ======================== 1 passed, 2 skipped in 0.04s =========================
五、 pytest.skip()方法内跳过
除了通过使用标签的方式,还可以在测试用例中调用pytest.skip()方法来实现跳过,可以选择传入reason参数来说明跳过原因;如果想要通过判断是否跳过,可以写在if判断里(_)
1 class TestClass(object): 2 3 def test001(self): 4 if 'h' in 'hell': 5 pytest.skip(reason='跳过,不执行') # 不执行后面的代码 6 print('test001') 7 8 def test002(self): 9 print('test002') 10 11 12 if __name__ == '__main__': 13 pytest.main(['-vs', 'test_skip.py'])
1 test_skip.py::TestClass::test001 SKIPPED (跳过,不执行) 2 Skipped: 跳过,不执行 3 4 test_skip.py::TestClass::test002 test002 5 PASSED 6 7 ======================== 1 passed, 1 skipped in 0.05s ======================
标签:总结,__,skip,py,pytest,test,跳过 来源: https://www.cnblogs.com/a-wyw/p/16198885.html