当前位置 : 主页 > 编程语言 > python >

系统学习Python——单元测试unittest:内置装饰器

来源:互联网 收集:自由互联 发布时间:2022-06-15
​ 在自动化测试过程中,我们可能会遇到这样的场景:在某些情况下,测试用例虽然不需要执行,但是我们又不愿意删掉它。下面来看看​​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


上一篇:【Python 教程】使用 Pandas 整理数据
下一篇:没有了
网友评论