单个测试用例
# -*- coding: utf-8 -*-
import unittest
from htmltestrunner import HTMLTestRunner
# 单个用例测试
class MyTestCase(unittest.TestCase):
# 启动
def setUp(self):
print '预测环境'
def testcase(self):
print '测试用例'
self.assertEqual(1,1,'断言失败')
def tearDown(self):
print '测试销毁'
if __name__ == '__main__':
unittest.main()
多个测试用例
# -*- coding: utf-8 -*-
import unittest
from htmltestrunner import HTMLTestRunner
# 多个用例测试
class TestCase1(unittest.TestCase):
def testcase1(self):
# 在这个地方做断言,比如说是调用一个接口,获得一个参数后和某个值做断言即可
self.assertEqual(1,2,'error!!!')
print 'this is testcase1'
def testcase2(self):
print 'this is testcase2'
class TestCase2(unittest.TestCase):
def testcase1(self):
print 'this is testcase1'
def testcase2(self):
print 'this is testcase2'
if __name__ == '__main__':
suit1 = unittest.TestLoader().loadTestsFromTestCase(TestCase1)
suit2 = unittest.TestLoader().loadTestsFromTestCase(TestCase2)
# 将多个测试用例放到测试集合里,方便测试多个
suit = unittest.TestSuite([suit1,suit2])
# verbosity 为测试用例个数
unittest.TextTestRunner(verbosity=2).run(suit)
# 打印出测试报告,使用HTMLTestRunner
filename = 'HTMLTestRunner.html'
f = open(filename , 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=f, title='report', description='测试用例报告')
runner.run(suit)