python + unittest + HTMLTestRunner

本文介绍了如何使用Python的unittest模块结合HTMLTestRunner生成详细的HTML测试报告。首先,需下载HTMLTestRunner.py文件并根据操作系统将其放置在相应Python库目录下。然后,通过示例展示如何使用该工具,最终生成的测试报告包含丰富的样式,并能展示截图。
摘要由CSDN通过智能技术生成

1、HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展。它生成易于使用的 HTML 测试报告

1>下载HTMLTestRunner.py文件,地址为:
http://tungwaiyip.info/software/HTMLTestRunner.html

2>安装过程
Windows平台:
将下载的文件放入…\Python27\Lib 目录下
Linux平台:
下需要先确定 python 的安装目录,打开终端,输入 python 命令进入 python 交互模式,通过 sys.path 可以查看本机 python 文件目录,以管理员身份将 HTMLTestRunner.py 文件考本到/usr/lib/python2.7/dist-packages/ 目录下。

3>使用范例

# -*- coding: utf-8 -*-
# @Time    : 2018/5/17 16:10
# @Author  : fmj
# @File    : fmjTest.py

# _*_ coding:utf-8 _*_
import unittest
import HTMLTestRunner

# 这句语句告诉Python,我们想要使用这个模块
import sys
# 一次性修改程序或系统的默认编码,重新加载sys这个模块。
reload(sys)
# 默认的编码是ascii,设置默认编码时使用utf-8
sys.setdefaultencoding("utf-8")

# 被测函数
def add(a, b):
c = a + b
print str(a) + "+"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 python+unittest+HTMLTestRunner 配置 HTML 测试报告的步骤如下: 1. 安装 HTMLTestRunner 模块 可以使用以下命令安装 HTMLTestRunner 模块: ``` pip install HTMLTestRunner ``` 2. 编写测试用例 使用 unittest 编写测试用例,例如: ```python import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': unittest.main() ``` 3. 编写 HTML 测试报告生成代码 编写 HTML 测试报告生成代码,例如: ```python import unittest import HTMLTestRunner class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(TestStringMethods('test_upper')) suite.addTest(TestStringMethods('test_isupper')) suite.addTest(TestStringMethods('test_split')) filename = 'test_report.html' fp = open(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='Test Report', description='This is a report of the test.' ) runner.run(suite) fp.close() ``` 4. 运行测试用例并生成 HTML 测试报告 运行测试用例并生成 HTML 测试报告,例如: ``` python test_report.py ``` 在当前目录下会生成一个名为 `test_report.html` 的 HTML 测试报告文件。打开该文件,即可查看测试结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值