HTMLTestRunner

Module: tests.HTMLTestRunner

A TestRunner for use with the Python unit testing framework. It generates a HTML 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 HTMLTestRunner

# ... define your tests ...

if __name__ == '__main__':
    HTMLTestRunner.main()

License

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, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name Wai Yip Tung nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Inheritance diagram for pySPACE.tests.HTMLTestRunner:

Inheritance diagram of pySPACE.tests.HTMLTestRunner

Class Summary

OutputRedirector(fp) Wrapper to redirect stdout or stderr
Template_mixin Define a HTML template for report customization and generation.
HTMLTestRunner([stream, verbosity, title, ...]) As opposed to the original script, two methods were added in order to account for the testing method used in the generic_unittest module.
TestProgram([module, defaultTest, argv, ...]) A variation of the unittest.TestProgram.

Classes

OutputRedirector

class pySPACE.tests.HTMLTestRunner.OutputRedirector(fp)[source]

Bases: object

Wrapper to redirect stdout or stderr

Class Components Summary

flush()
write(s)
writelines(lines)
__init__(fp)[source]
write(s)[source]
writelines(lines)[source]
flush()[source]
__weakref__

list of weak references to the object (if defined)

Template_mixin

class pySPACE.tests.HTMLTestRunner.Template_mixin[source]

Bases: object

Define a HTML template for report customization and generation.

Overall structure of an HTML report

<html>
  <head>

   STYLESHEET

  </head>

  <body>

   HEADING

   REPORT

   ENDING

  </body>
</html>

Class Components Summary

DEFAULT_DESCRIPTION
DEFAULT_TITLE
ENDING_TMPL
GENERIC_UNITTEST_HEADING
GENERIC_UNITTEST_TEST
HEADING_ATTRIBUTE_TMPL
HEADING_TMPL
HTML_TMPL
REPORT_CLASS_TMPL
REPORT_TEST_NO_OUTPUT_TMPL
REPORT_TEST_OUTPUT_TMPL
REPORT_TEST_WITH_OUTPUT_TMPL
REPORT_TMPL
STATUS
STYLESHEET_TMPL
SYSTEM_INFO
STATUS = {0: 'pass', 1: 'fail', 2: 'error'}
DEFAULT_TITLE = 'Unit Test Report'
DEFAULT_DESCRIPTION = ''
HTML_TMPL = '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <title>%(title)s</title>\n <meta name="generator" content="%(generator)s"/>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n %(stylesheet)s\n</head>\n<body>\n<script language="javascript" type="text/javascript"><!--\noutput_list = Array();\n\n/* level - 0:Summary; 1:Failed; 2:All */\nfunction showCase(level) {\n trs = document.getElementsByTagName("tr");\n for (var i = 0; i < trs.length; i++) {\n tr = trs[i];\n id = tr.id;\n if (id.substr(0,2) == \'ft\') {\n if (level < 1) {\n tr.className = \'hiddenRow\';\n }\n else {\n tr.className = \'\';\n }\n }\n if (id.substr(0,2) == \'pt\') {\n if (level > 1) {\n tr.className = \'\';\n }\n else {\n tr.className = \'hiddenRow\';\n }\n }\n }\n}\n\nfunction showTestDetail(div_id){\n var details_div = document.getElementById(div_id)\n var displayState = details_div.style.display\n // alert(displayState)\n if (displayState != \'block\' ) {\n displayState = \'block\'\n details_div.style.display = \'block\'\n }\n else {\n details_div.style.display = \'none\'\n }\n}\n\n\nfunction html_escape(s) {\n s = s.replace(/&/g,\'&amp;\');\n s = s.replace(/</g,\'&lt;\');\n s = s.replace(/>/g,\'&gt;\');\n return s;\n}\n\n/* obsoleted by detail in <div>\nfunction showOutput(id, name) {\n var w = window.open("", //url\n name,\n "resizable,scrollbars,status,width=800,height=450");\n d = w.document;\n d.write("<pre>");\n d.write(html_escape(output_list[id]));\n d.write("\\n");\n d.write("<a href=\'javascript:window.close()\'>close</a>\\n");\n d.write("</pre>\\n");\n d.close();\n}\n*/\n--></script>\n\n%(heading)s\n%(report)s\n%(ending)s\n\n</body>\n</html>\n'
STYLESHEET_TMPL = '\n<style type="text/css" media="screen">\nbody { font-family: verdana, arial, helvetica, sans-serif; font-size: 80%; }\ntable { font-size: 100%; }\npre { }\n\n/* -- heading ---------------------------------------------------------------------- */\nh1 {\n\tfont-size: 16pt;\n\tcolor: gray;\n}\n.heading {\n margin-top: 0ex;\n margin-bottom: 1ex;\n}\n\n.heading .attribute {\n margin-top: 1ex;\n margin-bottom: 0;\n}\n\n.heading .description {\n margin-top: 4ex;\n margin-bottom: 6ex;\n}\n\n/* -- css div popup ------------------------------------------------------------------------ */\na.popup_link {\n}\n\na.popup_link:hover {\n color: red;\n}\n\n.popup_window {\n display: none;\n position: relative;\n left: 0px;\n top: 0px;\n /*border: solid #627173 1px; */\n padding: 10px;\n background-color: #E6E6D6;\n font-family: "Lucida Console", "Courier New", Courier, monospace;\n text-align: left;\n font-size: 8pt;\n width: 500px;\n}\n\n}\n/* -- report ------------------------------------------------------------------------ */\n#show_detail_line {\n margin-top: 3ex;\n margin-bottom: 1ex;\n}\n#result_table {\n width: 80%;\n border-collapse: collapse;\n border: 1px solid #777;\n}\n#header_row {\n font-weight: bold;\n color: white;\n background-color: #777;\n}\n#result_table td {\n border: 1px solid #777;\n padding: 2px;\n}\n\n#info_table{\n border-collapse: collapse;\n border: 1px solid #777;\n width: 80%;\n}\n\n#info_table td {\n background-color: #310C3D;\n padding: 2px;\n}\n#info_row td {\n padding: 2px;\n background-color: white;\n border: 1px solid #151552;\n}\n#total_row { font-weight: bold; }\n.passClass { background-color: #6c6; }\n.failClass { background-color: #c60; }\n.errorClass { background-color: #c00; }\n.overallClass { background-color: #819FF7; }\n.totalClass { background-color: #F3F781; }\n.passCase { color: #6c6; }\n.failCase { color: #c60; font-weight: bold; }\n.errorCase { color: #c00; font-weight: bold; }\n.hiddenRow { display: none; }\n.testcase { margin-left: 2em; }\n\n\n/* -- ending ---------------------------------------------------------------------- */\n#ending {\n}\n\n</style>\n'
HEADING_TMPL = "<div class='heading'>\n<h1>%(title)s</h1>\n%(parameters)s\n<p class='description'>%(description)s</p>\n<p>%(sysinfo)s</p>\n<p id='show_detail_line'>Show\n<a href='javascript:showCase(0)'>Summary</a>\n<a href='javascript:showCase(1)'>Failed</a>\n<a href='javascript:showCase(2)'>All</a>\n</p>\n</div>\n\n"
GENERIC_UNITTEST_HEADING = "\n<table id='result_table'>\n<colgroup>\n<col align='left' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n</colgroup>\n<tr id='header_row'>\n <td>Test name</td>\n <td>Success</td>\n <td>Fail</td>\n <td>Error</td>\n</tr>\n%(test_list)s\n</table>\n"
GENERIC_UNITTEST_TEST = "\n<tr class='%(style)s'>\n <td>%(desc)s</td>\n <td>%(success)s</td>\n <td>%(fail)s</td>\n <td>%(error)s</td>\n</tr>\n"
HEADING_ATTRIBUTE_TMPL = "<p class='attribute'><strong>%(name)s:</strong> %(value)s</p>\n"
REPORT_TMPL = "\n<table id='result_table'>\n<colgroup>\n<col align='left' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n</colgroup>\n<tr id='header_row'>\n <td>Test Group/Test case</td>\n <td>Count</td>\n <td>Pass</td>\n <td>Fail</td>\n <td>Error</td>\n</tr>\n%(test_list)s\n<tr id='total_row'>\n <td>Total</td>\n <td>%(count)s</td>\n <td>%(Pass)s</td>\n <td>%(fail)s</td>\n <td>%(error)s</td>\n</tr>\n</table>\n"
REPORT_CLASS_TMPL = "\n<tr class='%(style)s'>\n <td>%(desc)s</td>\n <td>%(count)s</td>\n <td>%(Pass)s</td>\n <td>%(fail)s</td>\n <td>%(error)s</td>\n</tr>\n"
REPORT_TEST_WITH_OUTPUT_TMPL = '\n<tr id=\'%(tid)s\' class=\'%(Class)s\'>\n <td class=\'%(style)s\'><div class=\'testcase\'>%(desc)s</div></td>\n <td colspan=\'5\' align=\'center\'>\n\n <!--css div popup start-->\n <a class="popup_link" onfocus=\'this.blur();\' href="javascript:showTestDetail(\'div_%(tid)s\')" >\n %(status)s</a>\n\n <div id=\'div_%(tid)s\' class="popup_window">\n <div style=\'text-align: right; color:red;cursor:pointer\'>\n <a onfocus=\'this.blur();\' onclick="document.getElementById(\'div_%(tid)s\').style.display = \'none\' " >\n [x]</a>\n </div>\n <pre>\n %(script)s\n </pre>\n </div>\n <!--css div popup end-->\n\n </td>\n</tr>\n'
REPORT_TEST_NO_OUTPUT_TMPL = "\n<tr id='%(tid)s' class='%(Class)s'>\n <td class='%(style)s'><div class='testcase'>%(desc)s</div></td>\n <td colspan='5' align='center'>%(status)s</td>\n</tr>\n"
REPORT_TEST_OUTPUT_TMPL = '\n%(id)s: %(output)s\n'
SYSTEM_INFO = "\n<p>System information</p>\n<table id='info_table'>\n<colgroup>\n<col align='left' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n<col align='right' />\n</colgroup>\n<tr id='header_row'>\n <td>System</td>\n <td>Node</td>\n <td>Release</td>\n <td>Version</td>\n <td>Processor</td>\n <td>Python version</td>\n</tr>\n<tr id='info_row'>\n <td>%(system)s</td>\n <td>%(node)s</td>\n <td>%(release)s</td>\n <td>%(version)s</td>\n <td>%(processor)s</td>\n <td>%(pythonversion)s</td>\n</tr>\n</table>\n&nbsp;\n&nbsp;\n"
ENDING_TMPL = "<div id='ending'>&nbsp;</div>"
__weakref__

list of weak references to the object (if defined)

HTMLTestRunner

class pySPACE.tests.HTMLTestRunner.HTMLTestRunner(stream=<open file '<stdout>', mode 'w'>, verbosity=1, title=None, description=None)[source]

Bases: pySPACE.tests.HTMLTestRunner.Template_mixin

As opposed to the original script, two methods were added in order to account for the testing method used in the generic_unittest module.

Class Components Summary

_generate_ending()
_generate_generic_unittest_heading() provides methods of generating the yellow-blue table
_generate_heading(report_attrs)
_generate_report(result[, custom_decription])
_generate_report_test(rows, cid, tid, n, t, o, e)
_generate_stylesheet()
_generate_system_info()
_update_overall_results(new_result)
generateReport(result)
getReportAttributes(result) Return report attributes as a list of (name, value).
run(test)
sortResult(result_list)
__init__(stream=<open file '<stdout>', mode 'w'>, verbosity=1, title=None, description=None)[source]
run(test)[source]
sortResult(result_list)[source]
getReportAttributes(result)[source]

Return report attributes as a list of (name, value). Override this to add custom attributes.

generateReport(result)[source]
_generate_stylesheet()[source]
_generate_generic_unittest_heading()[source]

provides methods of generating the yellow-blue table at the beginning of the page

_generate_heading(report_attrs)[source]
_generate_system_info()[source]
_update_overall_results(new_result)[source]
_generate_report(result, custom_decription=None)[source]
_generate_report_test(rows, cid, tid, n, t, o, e)[source]
_generate_ending()[source]

TestProgram

class pySPACE.tests.HTMLTestRunner.TestProgram(module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=<unittest.loader.TestLoader object>, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None)[source]

Bases: unittest.main.TestProgram

A variation of the unittest.TestProgram. Please refer to the base class for command line parameters.

Class Components Summary

runTests()
runTests()[source]