分类目录:《系统学习Python》总目录
相关文章:
· 单元测试unittest:框架结构
· 单元测试unittest:测试固件
· 单元测试unittest:编写测试用例
· 单元测试unittest:执行测试用例
· 单元测试unittest:用例执行次序
· 单元测试unittest:内置装饰器
· 单元测试unittest:命令行执行测试
· 单元测试unittest:批量执行测试文件
· 单元测试unittest:测试断言
· 单元测试unittest:测试报告
在自动化测试过程中,我们可能会遇到这样的场景:在某些情况下,测试用例虽然不需要执行,但是我们又不愿意删掉它。下面来看看
unittest
提供的装饰器功能。
无条件跳过装饰器
下面的代码借助
@unittest.skip('skip info')
装饰器,演示无条件跳过执行某个方法:
import unittest
class MyTestClassOne(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
def test_001(self):
print('test_first')
self.assertEqual(0, 0)
@unittest.skip('skip info')
def test_002(self):
print('test_second')
self.assertEqual(0, 0)
def test_003(self):
print('test_third')
self.assertEqual(0, 0)
def test_004(self):
print('test_fourth')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
@classmethod
def tearDownClass(cls):
print('tearDownClass')
运行结果如下:
============================= test session starts =============================
collecting ... collected 4 items
testclass.py::MyTestClassOne::test_001 setUpClass
setUp
test_first
tearDown
testclass.py::MyTestClassOne::test_002 SKIPPED (skip info)
Skipped: skip info
testclass.py::MyTestClassOne::test_003
setUp
test_third
tearDown
testclass.py::MyTestClassOne::test_004
setUp
test_fourth
tearDown
tearDownClass
======================== 3 passed, 1 skipped in 0.03s =========================
Process finished with exit code 0
满足条件跳过装饰器
下面的代码借助
@unittest.skipIf(condition, 'info')
装饰器,演示当满足某个条件时,跳过执行某个方法:
import unittest
class MyTestClassOne(unittest.TestCase):
i = 10
@classmethod
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
def test_001(self):
print('test_first')
self.assertEqual(0, 0)
@unittest.skipIf(i > 1, 'info')
def test_002(self):
print('test_second')
self.assertEqual(0, 0)
def test_003(self):
print('test_third')
self.assertEqual(0, 0)
def test_004(self):
print('test_fourth')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
@classmethod
def tearDownClass(cls):
print('tearDownClass')
if __name__ == '__main__':
unittest.main()
因为变量
i = 1
,满足
i > 0
的条件,所以跳过执行
test_002
用例,运行结果如下:
============================= test session starts =============================
collecting ... collected 4 items
testclass.py::MyTestClassOne::test_001 setUpClass
PASSED [ 25%]setUp
test_first
tearDown
testclass.py::MyTestClassOne::test_002 SKIPPED (info) [ 50%]
Skipped: info
testclass.py::MyTestClassOne::test_003 PASSED [ 75%]setUp
test_third
tearDown
testclass.py::MyTestClassOne::test_004 PASSED [100%]setUp
test_fourth
tearDown
tearDownClass
======================== 3 passed, 1 skipped in 0.03s =========================
Process finished with exit code 0
不满足条件跳过
下面的代码借助@unittest.skipUnless(a==5,‘info’)装饰器,演示当不满足某个条件时,跳过执行某个方法。
import unittest
class MyTestClassOne(unittest.TestCase):
i = -1
@classmethod
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
self.i = 1
def test_001(self):
print('test_first')
self.assertEqual(0, 0)
def test_002(self):
print('test_second')
self.assertEqual(0, 0)
def test_003(self):
print('test_third')
self.assertEqual(0, 0)
@unittest.skipUnless(i > 0, 'info')
def test_004(self):
print('test_fourth')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
@classmethod
def tearDownClass(cls):
print('tearDownClass')
if __name__ == '__main__':
unittest.main()
因为
i < 0
不满足条件,所以跳过
test_004
用例,运行结果如下:
============================= test session starts =============================
collecting ... collected 4 items
testclass.py::MyTestClassOne::test_001
testclass.py::MyTestClassOne::test_002
testclass.py::MyTestClassOne::test_003
testclass.py::MyTestClassOne::test_004
======================== 3 passed, 1 skipped in 0.03s =========================
Process finished with exit code 0
setUpClass
setUp
test_first
tearDown
setUp
test_second
tearDown
setUp
test_third
tearDown
SKIPPED (info)
Skipped: info
tearDownClass
版权归原作者 von Neumann 所有, 如有侵权,请联系我们删除。