43

How can I generate test report using pytest? I searched for it but whatever I got was about coverage report. I tried with this command:

py.test sanity_tests.py --cov=C:\Test\pytest --cov-report=xml

But as parameters represents it generates coverage report not test report.

Neuron
  • 5,141
  • 5
  • 38
  • 59
fhulprogrammer
  • 599
  • 2
  • 7
  • 16
  • What test report are you expecting? – jonrsharpe Mar 18 '15 at 13:54
  • @jonrsharpe test report like tests successful or unsuccessful. If unsuccessful then message which is passed in assert statements. – fhulprogrammer Mar 18 '15 at 14:51
  • Do you just mean what is shown in the terminal? Why not simply `>` it to a file? – jonrsharpe Mar 18 '15 at 14:52
  • @jonrsharpe as pytest generates coverage report in defined clean way, i'm wondering is there any property of pytest which can be used to generate test report in xml format. using > it will generate a text format. XML format will be easy to parse and get results store for future use. – fhulprogrammer Mar 18 '15 at 15:27
  • The XML coverage report is **not** generated by `py.test`, it's generated by [`coverage`](http://nedbatchelder.com/code/coverage/) via the `pytest-cov` plugin. As far as I know, there isn't similar functionality built into `py.test` itself. I don't know what Allure is, but this plugin refers to XML reporting: https://pypi.python.org/pypi/pytest-allure-adaptor – jonrsharpe Mar 18 '15 at 15:29
  • @jonrsharpe thanks. Is there any other module or package which can be used to do so? – fhulprogrammer Mar 18 '15 at 15:32
  • Please do some research, I have not had this requirement so don't know. – jonrsharpe Mar 18 '15 at 15:32
  • Do you mean a XML file with the test results? Did you try the --junitxml option? – Bruno Oliveira Mar 18 '15 at 15:40
  • @BrunoOliveira no. how to use it? – fhulprogrammer Mar 18 '15 at 15:54
  • 1
    @BrunoOliveira I tried using `py.test sample_tests.py --junitxml=C:\path` but it's throwing error `PermissionError: [Errno 13] Permission denied: 'C:\\path'` – fhulprogrammer Mar 18 '15 at 16:04
  • @fhulprogrammer have you tried it with a directory that exists and has appropriate permissions? – jonrsharpe Mar 18 '15 at 16:11
  • @jonrsharpe Yes directory exists and I also gave Read/Write permission to everyone. But same error `Permission denied` – fhulprogrammer Mar 18 '15 at 16:24
  • 1
    @jonrsharpe try using `--junitxml=c:\path\reports.xml`. – Bruno Oliveira Mar 19 '15 at 01:26
  • @jonrsharpe is there any command line option to generate report in html format? I can parse XML and dump contents to html but wondering if this feature already available in pytest. I tried command `--junithtml` but it's not valid parameter. – fhulprogrammer Mar 31 '15 at 09:23
  • @fhulprogrammer I don't know – jonrsharpe Mar 31 '15 at 09:24
  • @fhulprogrammer there's the [pytest-html](https://pypi.python.org/pypi/pytest-html) plugin for that. Unfortunately it seems to truncate output for test failures, which made it useless for my purposes. – akaihola Mar 28 '18 at 08:52

4 Answers4

80

Ripped from the comments: you can use the --junitxml argument.

$ py.test sample_tests.py --junitxml=C:\path\to\out_report.xml
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • Per the comments this seemed to solve the OP's problem. Marked as Community Wiki since I just paraphrased what @BrunoOliveira wrote in the comments. – Adam Smith May 14 '15 at 06:39
  • 3
    Documented [here](https://docs.pytest.org/en/latest/usage.html#creating-junitxml-format-files) – Carl G May 17 '19 at 04:08
  • 7
    Documentation link update: https://docs.pytest.org/en/latest/how-to/output.html#creating-junitxml-format-files – maciek Apr 09 '21 at 08:36
6

You can use a pytest plugin 'pytest-html' for generating html reports which can be forwarded to different teams as well

First install the plugin:

$ pip install pytest-html

Second, just run your tests with this command:

$ pytest --html=report.html

You can also make use of the hooks provided by the plugin in your code.

import pytest
from py.xml import html

def pytest_html_report_title(report)
   report.title = "My very own title!"

Reference: https://pypi.org/project/pytest-html/

Libin Thomas
  • 829
  • 9
  • 18
0

I haven't tried it yet but you can try referring to https://github.com/pytest-dev/pytest-html. A python library that can generate HTML output.

user3382968
  • 378
  • 4
  • 16
-2
py.test --html=Report.html 

Here you can specify your python file as well. In this case, when there is no file specified it picks up all the files with a name like 'test_%' present in the directory where the command is run and executes them and generates a report with the name Report.html

You can also modify the name of the report accordingly.

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73