How to generate test report using pytest?
Asked Answered
C

4

44

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.

Cholent answered 18/3, 2015 at 13:48 Comment(16)
What test report are you expecting?Monkhood
@Monkhood test report like tests successful or unsuccessful. If unsuccessful then message which is passed in assert statements.Cholent
Do you just mean what is shown in the terminal? Why not simply > it to a file?Monkhood
@Monkhood 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.Cholent
The XML coverage report is not generated by py.test, it's generated by 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: pypi.python.org/pypi/pytest-allure-adaptorMonkhood
@Monkhood thanks. Is there any other module or package which can be used to do so?Cholent
Please do some research, I have not had this requirement so don't know.Monkhood
Do you mean a XML file with the test results? Did you try the --junitxml option?Hopeh
@BrunoOliveira no. how to use it?Cholent
@BrunoOliveira I tried using py.test sample_tests.py --junitxml=C:\path but it's throwing error PermissionError: [Errno 13] Permission denied: 'C:\\path'Cholent
@Cholent have you tried it with a directory that exists and has appropriate permissions?Monkhood
@Monkhood Yes directory exists and I also gave Read/Write permission to everyone. But same error Permission deniedCholent
@Monkhood try using --junitxml=c:\path\reports.xml.Hopeh
@Monkhood 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.Cholent
@Cholent I don't knowMonkhood
@Cholent there's the pytest-html plugin for that. Unfortunately it seems to truncate output for test failures, which made it useless for my purposes.Authentic
B
83

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

$ py.test sample_tests.py --junitxml=C:\path\to\out_report.xml
Biedermeier answered 18/3, 2015 at 13:48 Comment(3)
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.Biedermeier
Documented hereAdar
Documentation link update: docs.pytest.org/en/latest/how-to/…Unroof
A
8

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/

Ace answered 15/10, 2020 at 15:59 Comment(0)
N
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.

Nkrumah answered 16/2, 2020 at 15:13 Comment(0)
M
-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.

Manger answered 22/5, 2020 at 19:24 Comment(1)
A vanilla install of pytest did not seem to have this capability in it.Hygroscopic

© 2022 - 2024 — McMap. All rights reserved.