【单元测试】unittest + HTMLTestRunner 实现报告可视化:

HTMLTestRunner: https://blog.csdn.net/liujingqiu/article/details/50518776

测试报告优化 https://blog.csdn.net/FloraCHY/article/details/80069737

单元测试: https://blog.csdn.net/xiaosongbk/article/details/52894522

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

例子:

def add(a, b):

return a+b

 

def minus(a, b):

return a-b

 

def multi(a, b):

return a*b

 

def divide(a, b):

return a/b

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

单元测试:

import unittest

from liz import *

from BSTestRunner import BSTestRunner

import time

 

class Test(unittest.TestCase):

"""Test mathfuc.py"""

 

def test_add(self):

"""Test method add(a, b)"""

self.assertEqual(3, add(1, 2))

self.assertNotEqual(3, add(2, 2))

 

def test_minus(self):

"""Test method minus(a, b)"""

self.assertEqual(1, minus(3, 2))

 

def test_multi(self):

"""Test method multi(a, b)"""

self.assertEqual(6, multi(2, 3))

 

def test_divide(self):

"""Test method divide(a, b)"""

self.assertEqual(2, divide(6, 3))

self.assertEqual(2.5, divide(5, 2))

 

if __name__ == '__main__':

# unittest.main()

 

suite=unittest.TestSuite()

suite.addTest(Test("test_add"))

suite.addTest(Test("test_minus"))

suite.addTest(Test("test_multi"))

suite.addTest(Test("test_divide"))

now=time.strftime("%Y-%m-%d %H_%M_%S")

#定义报告存放路径

filename='D:\jing\dycs\log'+ now +'result.html'

fp=open(filename,'wb')

#定义测试报告

runner=BSTestRunner(stream=fp,title='测试报告',description='用例执行情况:')

runner.run(suite)

fp.close()#关闭报告文件

你可能感兴趣的:(【单元测试】unittest + HTMLTestRunner 实现报告可视化:)