分类目录:《系统学习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’)装饰器,演示当不满足某个条件时,跳过执行某个方法。文章来源:https://www.toymoban.com/news/detail-412583.html
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
用例,运行结果如下:文章来源地址https://www.toymoban.com/news/detail-412583.html
============================= 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
到了这里,关于系统学习Python——单元测试unittest:内置装饰器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!