注:
(1)、unittest.main()执行用例的顺序:按照用例方法test后字符的ASCII码顺序来执行用例顺序
(2)、手动确定用例执行顺序:使用测试套件unittest.TestSuite()
# -*- coding:utf-8 -*-
import unittest
class UserCase(unittest.TestCase):
def setUp(self):
print("set Up 开始执行")
def tearDown(self):
print("tear Down 执行结束")
def testcase1(self):
print("UserCase testcase1")
def testcase2(self):
print("UserCase testcase2")
self.assertEqual(1,1)
def testcase3(self):
print("UserCase testcase3")
if __name__=="__main__":
unittest.main()
# -*- coding:utf-8 -*-
import unittest
from UserTestCase import UserCase
class UserCase2(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("setUpClass 开始执行")
@classmethod
def tearDownClass(cls):
print("setUpClass 执行结束")
def testcase1(self):
print("UserCase2 testcase1")
def testcase2(self):
print("UserCase2 testcase2")
self.assertEqual(1,1)
def testcase3(self):
print("UserCase2 testcase3")
if __name__=="__main__":
#测试套件添加用例单个单个添加 ,类名('方法名') 格式添加单个用例
suit=unittest.TestSuite()
suit.addTest(UserCase2('testcase1'))
suit.addTest(UserCase2('testcase2'))
suit.addTest(UserCase('testcase1')) #还可以添加其他类中的用例
#批量添加测试用例:suit.addTests([列表])
suit = unittest.TestSuite()
suit.addTests([UserCase2('testcase1'),UserCase2('testcase2'),UserCase('testcase1')])
#verbosity=2代表输出用例执行的结果日志级别为2:最详细
runner=unittest.TextTestRunner(verbosity=2)
#运行测试套件中添加的所有用例
runner.run(suit)
"""
执行结果:
setUpClass 开始执行
UserCase2 testcase1
UserCase2 testcase2
setUpClass 执行结束
set Up 开始执行
UserCase testcase1
tear Down 执行结束
testcase1 (__main__.UserCase2) ... ok
testcase2 (__main__.UserCase2) ... ok
testcase1 (UserTestCase.UserCase) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
"""
网友评论