11-unittest

unittest测试框架

import unittest,HTMLTestRunner,xmlrunner


class TestClas(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("setup_class")

@classmethod
def tearDownClass(cls):
print("tear_down")

def setUp(self):
print("每个测试用例开始前执行setup")

def tearDown(self):
print("每个测试用例结束,执行teardown")

def test_a_case(self):
'''测试用例a''' #三引号注释会显示在测试报告中
self.assertEqual(1,1)

def test_b_case(self):
'''测试用例b'''
self.assertEqual(1,1)

def test_c_case(self):
'''测试用例c'''
self.assertEqual(1,1)

class TestClas2(unittest.TestCase):
def test_a_case(self):
'''TestClas2测试用例a'''
self.assertEqual(1, 1)

def test_b_case(self):
'''TestClas2测试用例b'''
self.assertEqual(1, 2)

def test_c_case(self):
'''TestClas2测试用例c'''
self.assertEqual(1, 2)


if __name__ == '__main__':
# unittest.main() #运行所有的用例
suite = unittest.TestSuite() #创建测试套件
# suite.addTest(TestClas('test_a_case')) #添加类中某一个方法到测试套件中
# suite.addTest(TestClas2('test_b_case')) #添加其他类中某一个方法到测试套件中
suite.addTest(unittest.makeSuite(TestClas)) #把整个类作为suite传进套件
suite.addTest(unittest.makeSuite(TestClas2)) #把整个类作为suite传进套件
# ---------生成html报告----------------
# html_rep = open(r'/Users/dalei/Desktop/boulder/Boulder/Autotest/reports/report.html','wb')
# runner = HTMLTestRunner.HTMLTestRunner(stream=html_rep,title='报告标题',description='测试内容')
# runner.run(suite)
# ---------生成xml报告,为jenkins持续集成使用----------------
xml_runner = xmlrunner.XMLTestRunner(output='/Users/dalei/Desktop/boulder/Boulder/Autotest/reports/')
xml_runner.run(suite)
---------------setup---------teardowm---------------------
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# 测试类运行之前运行的
print("类set")
@classmethod
def tearDownClass(cls):
# 测试类运行之后运行的
print("类tear")
def setUp(self):
# 每个用例运行之前会运行
print("每个用例运行之前会运行")
def tearDown(self):
#每个用例之后会运行
print("每个用例运行之后会运行")
--------------------------添加目录下的case-----------------------
import unittest
import HTMLTestRunner
suite = unittest.TestSuite()

all_case = unittest.defaultTestLoader.discover(
r'/Users/dalei/Desktop/boulder/Boulder/AUTO/case/','*.py'
)
#找到case目录下所有.py文件 添加测试用例

for case in all_case:
print('case...',case)
suite.addTests(case)


fw = open('report.html','wb')

runner = HTMLTestRunner.HTMLTestRunner(
stream=fw,title='多个用例文件'
)

runner.run(suite)
# runner.run(all_case)

posted on 2017-12-21 22:16  大磊爱吃又  阅读(110)  评论(0编辑  收藏  举报

导航