在自动化测试过程中,我们可能会遇到这样的场景:在某些情况下,测试用例虽然不需要执行,但是我们又不愿意删掉它。下面来看看unittest提供的装饰器功能。 无条件跳过
在自动化测试过程中,我们可能会遇到这样的场景:在某些情况下,测试用例虽然不需要执行,但是我们又不愿意删掉它。下面来看看unittest提供的装饰器功能。
无条件跳过装饰器
下面的代码借助@unittest.skip('skip info')装饰器,演示无条件跳过执行某个方法:
import unittestclass 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 unittestclass 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 unittestclass 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