DESCRIPTION.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. pytest-html
  2. ===========
  3. pytest-html is a plugin for `pytest <http://pytest.org>`_ that generates a
  4. HTML report for the test results.
  5. .. image:: https://img.shields.io/badge/license-MPL%202.0-blue.svg
  6. :target: https://github.com/pytest-dev/pytest-html/blob/master/LICENSE
  7. :alt: License
  8. .. image:: https://img.shields.io/pypi/v/pytest-html.svg
  9. :target: https://pypi.python.org/pypi/pytest-html/
  10. :alt: PyPI
  11. .. image:: https://img.shields.io/travis/pytest-dev/pytest-html.svg
  12. :target: https://travis-ci.org/pytest-dev/pytest-html/
  13. :alt: Travis
  14. .. image:: https://img.shields.io/github/issues-raw/pytest-dev/pytest-html.svg
  15. :target: https://github.com/pytest-dev/pytest-html/issues
  16. :alt: Issues
  17. .. image:: https://img.shields.io/requires/github/pytest-dev/pytest-html.svg
  18. :target: https://requires.io/github/pytest-dev/pytest-html/requirements/?branch=master
  19. :alt: Requirements
  20. Requirements
  21. ------------
  22. You will need the following prerequisites in order to use pytest-html:
  23. - Python 2.7, 3.6, PyPy, or PyPy3
  24. Installation
  25. ------------
  26. To install pytest-html:
  27. .. code-block:: bash
  28. $ pip install pytest-html
  29. Then run your tests with:
  30. .. code-block:: bash
  31. $ pytest --html=report.html
  32. ANSI codes
  33. ----------
  34. Note that ANSI code support depends on the
  35. `ansi2html <https://pypi.python.org/pypi/ansi2html/>`_ package. Due to the use
  36. of a less permissive license, this package is not included as a dependency. If
  37. you have this package installed, then ANSI codes will be converted to HTML in
  38. your report.
  39. Creating a self-contained report
  40. --------------------------------
  41. In order to respect the `Content Security Policy (CSP)
  42. <https://developer.mozilla.org/docs/Web/Security/CSP>`_,
  43. several assets such as CSS and images are stored separately by default.
  44. You can alternatively create a self-contained report, which can be more
  45. convenient when sharing your results. This can be done in the following way:
  46. .. code-block:: bash
  47. $ pytest --html=report.html --self-contained-html
  48. Images added as files or links are going to be linked as external resources,
  49. meaning that the standalone report HTML-file may not display these images
  50. as expected.
  51. The plugin will issue a warning when adding files or links to the standalone report.
  52. Enhancing reports
  53. -----------------
  54. Environment
  55. ~~~~~~~~~~~
  56. The *Environment* section is provided by the `pytest-metadata
  57. <https://pypi.python.org/pypi/pytest-metadata/>`_, plugin, and can be accessed
  58. via the :code:`pytest_configure` hook:
  59. .. code-block:: python
  60. def pytest_configure(config):
  61. config._metadata['foo'] = 'bar'
  62. Extra content
  63. ~~~~~~~~~~~~~
  64. You can add details to the HTML reports by creating an 'extra' list on the
  65. report object. Here are the types of extra content that can be added:
  66. ========== ============================================
  67. Type Example
  68. ========== ============================================
  69. Raw HTML ``extra.html('<div>Additional HTML</div>')``
  70. `JSON`_ ``extra.json({'name': 'pytest'})``
  71. Plain text ``extra.text('Add some simple Text')``
  72. URL ``extra.url('http://www.example.com/')``
  73. Image ``extra.image(image, mime_type='image/gif', extension='gif')``
  74. Image ``extra.image('/path/to/file.png')``
  75. Image ``extra.image('http://some_image.png')``
  76. ========== ============================================
  77. **Note**: When adding an image from file, the path can be either absolute
  78. or relative.
  79. **Note**: When using ``--self-contained-html``, images added as files or links
  80. may not work as expected, see section `Creating a self-contained report`_ for
  81. more info.
  82. There are also convenient types for several image formats:
  83. ============ ====================
  84. Image format Example
  85. ============ ====================
  86. PNG ``extra.png(image)``
  87. JPEG ``extra.jpg(image)``
  88. SVG ``extra.svg(image)``
  89. ============ ====================
  90. The following example adds the various types of extras using a
  91. :code:`pytest_runtest_makereport` hook, which can be implemented in a plugin or
  92. conftest.py file:
  93. .. code-block:: python
  94. import pytest
  95. @pytest.mark.hookwrapper
  96. def pytest_runtest_makereport(item, call):
  97. pytest_html = item.config.pluginmanager.getplugin('html')
  98. outcome = yield
  99. report = outcome.get_result()
  100. extra = getattr(report, 'extra', [])
  101. if report.when == 'call':
  102. # always add url to report
  103. extra.append(pytest_html.extras.url('http://www.example.com/'))
  104. xfail = hasattr(report, 'wasxfail')
  105. if (report.skipped and xfail) or (report.failed and not xfail):
  106. # only add additional html on failure
  107. extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
  108. report.extra = extra
  109. You can also specify the :code:`name` argument for all types other than :code:`html` which will change the title of the
  110. created hyper link:
  111. .. code-block:: python
  112. extra.append(pytest_html.extras.text('some string', name='Different title'))
  113. Modifying the results table
  114. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. You can modify the columns by implementing custom hooks for the header and
  116. rows. The following example :code:`conftest.py` adds a description column with
  117. the test function docstring, adds a sortable time column, and removes the links
  118. column:
  119. .. code-block:: python
  120. from datetime import datetime
  121. from py.xml import html
  122. import pytest
  123. @pytest.mark.optionalhook
  124. def pytest_html_results_table_header(cells):
  125. cells.insert(2, html.th('Description'))
  126. cells.insert(0, html.th('Time', class_='sortable time', col='time'))
  127. cells.pop()
  128. @pytest.mark.optionalhook
  129. def pytest_html_results_table_row(report, cells):
  130. cells.insert(2, html.td(report.description))
  131. cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
  132. cells.pop()
  133. @pytest.mark.hookwrapper
  134. def pytest_runtest_makereport(item, call):
  135. outcome = yield
  136. report = outcome.get_result()
  137. report.description = str(item.function.__doc__)
  138. You can also remove results by implementing the
  139. :code:`pytest_html_results_table_row` hook and removing all cells. The
  140. following example removes all passed results from the report:
  141. .. code-block:: python
  142. import pytest
  143. @pytest.mark.optionalhook
  144. def pytest_html_results_table_row(report, cells):
  145. if report.passed:
  146. del cells[:]
  147. The log output and additional HTML can be modified by implementing the
  148. :code:`pytest_html_results_html` hook. The following example replaces all
  149. additional HTML and log output with a notice that the log is empty:
  150. .. code-block:: python
  151. import pytest
  152. @pytest.mark.optionalhook
  153. def pytest_html_results_table_html(report, data):
  154. if report.passed:
  155. del data[:]
  156. data.append(html.div('No log output captured.', class_='empty log'))
  157. Screenshots
  158. -----------
  159. .. image:: https://cloud.githubusercontent.com/assets/122800/11952194/62daa964-a88e-11e5-9745-2aa5b714c8bb.png
  160. :target: https://cloud.githubusercontent.com/assets/122800/11951695/f371b926-a88a-11e5-91c2-499166776bd3.png
  161. :alt: Enhanced HTML report
  162. Contributing
  163. ------------
  164. Fork the repository and submit PRs with bug fixes and enhancements, contributions are very welcome.
  165. Tests can be run locally with `tox`_, for example to execute tests for Python 2.7 and 3.6 execute::
  166. tox -e py27,py36
  167. .. _`tox`: https://tox.readthedocs.org/en/latest/
  168. Resources
  169. ---------
  170. - `Release Notes <http://github.com/pytest-dev/pytest-html/blob/master/CHANGES.rst>`_
  171. - `Issue Tracker <http://github.com/pytest-dev/pytest-html/issues>`_
  172. - `Code <http://github.com/pytest-dev/pytest-html/>`_
  173. .. _JSON: http://json.org/