unittest+python3.x使用HTMLTestRunner输出html测试报告

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31524409/article/details/80984729

这里使用的是python3.x,则需要相应的HTMLTestRunner.py文件,否则不能输出html测试报告

HTMLTestRunner.py文件下载连接如下

       https://github.com/linyuli861/Automated-Test.git
      

将下载的文件放入python安装目录下的Lib目录即可

示例如下:

先设置应用的函数文件mathfunc.py

def add(a,b):
	return a+b

然后编写测试脚本文件test_mathfunc.py

import unittest
from mathfunc import *
import HTMLTestRunner

class TestMathFunc(unittest.TestCase):

	def test_01(self):
		self.assertEqual(3,add(1,4))

	def test_02(self):
		self.assertEqual(5,add(1,4))

if __name__ == '__main__':
	filepath = '../report/htmlreport.html'
	ftp=open(filepath,'wb')
	suite = unittest.TestSuite()
	suite.addTest(TestMathFunc('test_01'))
	suite.addTest(TestMathFunc('test_02'))
	runner=HTMLTestRunner.HTMLTestRunner(stream=ftp,title='welcome to this web')
	runner.run(suite)
	unittest.main()

filepath的参数是指定输出测试报告html文件的位置。

ftp表示打开文件路径,‘wb’是参数,不可省略。

猜你喜欢

转载自blog.csdn.net/qq_31524409/article/details/80984729
今日推荐