【Python】【unittest】unittest测试框架中setup,teardown与setupclass,teardownclass的区别

# -*- coding:utf-8 -*-
import unittest


def runTest(testcaseclass,testcase=[]):
suite = unittest.TestSuite()
for case in testcase:
suite.addTest(testcaseclass(case))
unittest.TextTestRunner().run(suite)

class test(unittest.TestCase):

def setUp(self):
print 'This is the setup msg'

def tearDown(self):
print 'This is the teardown msg'
@classmethod
def setUpClass(cls):
print 'this is the setupclass msg'


@classmethod
def tearDownClass(cls):
print 'this is the teardownclass msg'

def test1(self):
print 'This is the first testcase'

def test2(self):
print 'This is the second testcase'

def test3(self):
print 'This is the third testcase'

runTest(test,['test2','test3','test1'])


输出如下:

C:\Python27\python.exe "E:/Project/A3A 8 4G/13.py"
...

----------------------------------------------------------------------


this is the setupclass msg
This is the setup msg
This is the second testcase

This is the teardown msg

This is the setup msg
This is the third testcase
This is the teardown msg
This is the setup msg
This is the first testcase
This is the teardown msg
this is the teardownclass msg

Ran 3 tests in 0.000s

OK

Process finished with exit code 0

总结:

1、setup()和teardown()两个函数在每条测试用例执行时都会进行重复执行一次,该场景针对那些测试用例间有相互影响的场景,才需要在每执行一条新用例时进行一次初使化,执行完毕后再清空所有配置

2、setupclass(cls)和teardownclass(cls)两个函数在一个用例集合执行前只会执行一次,然后再所有用例执行完成后再清空所有配置,此种用法主要用在用例之间相互没有什么影响的场景

猜你喜欢

转载自www.cnblogs.com/51study/p/9284519.html
今日推荐