当前位置:   article > 正文

python3下的使用 HTMLTestRunner_win下htmltestrunner_py3使用

win下htmltestrunner_py3使用
  1. """
  2. A TestRunner for use with the Python unit testing framework. It
  3. generates a HTML report to show the result at a glance.
  4. The simplest way to use this is to invoke its main method. E.g.
  5. import unittest
  6. import HTMLTestRunner
  7. ... define your tests ...
  8. if __name__ == '__main__':
  9. HTMLTestRunner.main()
  10. For more customization options, instantiates a HTMLTestRunner object.
  11. HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g.
  12. # output to a file
  13. fp = file('my_report.html', 'wb')
  14. runner = HTMLTestRunner.HTMLTestRunner(
  15. stream=fp,
  16. title='My unit test',
  17. description='This demonstrates the report output by HTMLTestRunner.'
  18. )
  19. # Use an external stylesheet.
  20. # See the Template_mixin class for more customizable options
  21. runner.STYLESHEET_TMPL = '<link rel="stylesheet" href="my_stylesheet.css" type="text/css">'
  22. # run the test
  23. runner.run(my_test_suite)
  24. ------------------------------------------------------------------------
  25. Copyright (c) 2004-2007, Wai Yip Tung
  26. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions are
  29. met:
  30. * Redistributions of source code must retain the above copyright notice,
  31. this list of conditions and the following disclaimer.
  32. * Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. * Neither the name Wai Yip Tung nor the names of its contributors may be
  36. used to endorse or promote products derived from this software without
  37. specific prior written permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  39. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  40. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  41. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  42. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  45. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  46. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  47. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. """
  50. # URL: http://tungwaiyip.info/software/HTMLTestRunner.html
  51. __author__ = "Wai Yip Tung"
  52. __version__ = "0.8.2"
  53. """
  54. Change History
  55. Version 0.8.2
  56. * Show output inline instead of popup window (Viorel Lupu).
  57. Version in 0.8.1
  58. * Validated XHTML (Wolfgang Borgert).
  59. * Added description of test classes and test cases.
  60. Version in 0.8.0
  61. * Define Template_mixin class for customization.
  62. * Workaround a IE 6 bug that it does not treat <script> block as CDATA.
  63. Version in 0.7.1
  64. * Back port to Python 2.3 (Frank Horowitz).
  65. * Fix missing scroll bars in detail log (Podi).
  66. """
  67. # TODO: color stderr
  68. # TODO: simplify javascript using ,ore than 1 class in the class attribute?
  69. import datetime
  70. import io
  71. import sys
  72. import time
  73. import unittest
  74. from xml.sax import saxutils
  75. # ------------------------------------------------------------------------
  76. # The redirectors below are used to capture output during testing. Output
  77. # sent to sys.stdout and sys.stderr are automatically captured. However
  78. # in some cases sys.stdout is already cached before HTMLTestRunner is
  79. # invoked (e.g. calling logging.basicConfig). In order to capture those
  80. # output, use the redirectors for the cached stream.
  81. #
  82. # e.g.
  83. # >>> logging.basicConfig(stream=HTMLTestRunner.stdout_redirector)
  84. # >>>
  85. class OutputRedirector(object):
  86. """ Wrapper to redirect stdout or stderr """
  87. def __init__(self, fp):
  88. self.fp = fp
  89. def write(self, s):
  90. self.fp.write(bytes(s,'UTF-8'))
  91. def writelines(self, lines):
  92. self.fp.writelines(lines)
  93. def flush(self):
  94. self.fp.flush()
  95. stdout_redirector = OutputRedirector(sys.stdout)
  96. stderr_redirector = OutputRedirector(sys.stderr)
  97. # ----------------------------------------------------------------------
  98. # Template
  99. class Template_mixin(object):
  100. """
  101. Define a HTML template for report customerization and generation.
  102. Overall structure of an HTML report
  103. HTML
  104. +------------------------+
  105. |<html> |
  106. | <head> |
  107. | |
  108. | STYLESHEET |
  109. | +----------------+ |
  110. | | | |
  111. | +----------------+ |
  112. | |
  113. | </head> |
  114. | |
  115. | <body> |
  116. | |
  117. | HEADING |
  118. | +----------------+ |
  119. | | | |
  120. | +----------------+ |
  121. | |
  122. | REPORT |
  123. | +----------------+ |
  124. | | | |
  125. | +----------------+ |
  126. | |
  127. | ENDING |
  128. | +----------------+ |
  129. | |
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号