Python unittest如如何何生生成成HTMLTestRunner模模块块
这篇文章主要介绍了Python unittest如何生成HTMLTestRunner模块,文中通过示例代码介绍的非常详细,对大家的
学习或者工作具有一 的参考学习价值,需要的朋友可以参考下
生成 HTMLTestRunner 模块
unittest 里面是不能生成 html 格式报告的,需要导入一个第三方的模块 :HTMLTestRunner
方法1.这个模块下载不能通过 pip 安装了,只能下载后手动导入,下载地址 :
/software/HTMLTestRunner.html
方法2.在 python 安装文件的 Lib 目录下新增文件 HTMLTestRunner.py
两种模板如下,建议使用第一种 (第一种模板更加美观)
文件内容如下 :
(1)第一种模板
# -*- coding: utf-8 -*-
"""
A TestRunner for use with the Python unit testing framework. It
generates a TML report to show the result at a glance.
The simplest way to use this is to invoke its main method. E.g.
import unittest
import TMLTestRunner
... define your tests ...
if __name__ == '__main__ ':
TMLTestRunner.main()
For more customization options, instantiates a TMLTestRunner object.
TMLTestRunner is a counterpart to unittest 's TextTestRunner. E.g.
# output to a file
fp = file('my_report.html', 'wb')
runner = TMLTestRunner. TMLTestRunner(
stream=fp,
title='My unit test ',
description='This demonstrates the report output by TMLTestRunner.'
)
# Use an external stylesheet.
# See the Template_mixin class for more customizable options
runner.STYLES EET_TMPL = ''
# run the test
runner.run(my_test_suite)
Copyright (c) 2004-2007, Wai Yip Tung
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,