Skip to content

How to parse py.test output? #2306

Closed
Closed

Description

@emin63

What is the recommended way to parse py.test output?

At first I thought --junitxml was the way to go but it seems like that is incompatible with the xdist plugin and also not documented much.

Then I thought pytest-json looked great, but it seems to break reading a setup.cfg file.

Is there a simple way to be able to collect the output of a py.test run in a machine readable format for easy reporting?

Thanks.

Activity

  1. nicoddemus commented on Mar 14, 2017

    @nicoddemus
    Member

    Hi,

    At first I thought --junitxml was the way to go but it seems like that is incompatible with the xdist plugin and also not documented much.

    That's not true, pytest with xdist and the junitxml option work as expected (all our tests at work use both successfully). Can you post what error are you having, if any?

    Then I thought pytest-json looked great, but it seems to break reading a setup.cfg file.

    If so it might be a bug in pytest-json, could you please report the problem in the appropriate channel?

    Is there a simple way to be able to collect the output of a py.test run in a machine readable format for easy reporting?

    Besides junitxml there's also --result-log but that's been deprecated for some time, although we don't have a set timeline to remove it yet.

  2. added
    type: question
    on Mar 14, 2017
  3. emin63 commented on Mar 14, 2017

    @emin63
    Author

    Thanks for the quick response.

    Perhaps I was confused about junitxml. Looking at http://doc.pytest.org/en/latest/usage.html, it seems like there is a comment that it is experimental and it does not work with pytest-xdist. Maybe that was referring to record_xml_property and not junitxml?

    Issue filed for pytest-json at mattcl/pytest-json#2.

    If junitxml is the way to go, could you point me to any tools or specifications for the output format?

    Thanks again.

  4. nicoddemus commented on Mar 14, 2017

    @nicoddemus
    Member

    Maybe that was referring to record_xml_property and not junitxml?

    Exactly. 😁

    I have updated the docs to make the distinction clearer, see #2307.

    Issue filed for pytest-json at mattcl/pytest-json#2.

    Thanks, appreciate it.

    If junitxml is the way to go, could you point me to any tools or specifications for the output format?

    If you execute it with a few tests the format should be self-explanatory, but you can see the official schema here.

  5. RonnyPfannschmidt commented on Mar 14, 2017

    @RonnyPfannschmidt
    Member

    the basic answer is - there is absolutely no sanely parsable and complete output for pytest

    the problems with xdist vs junitxml probably relate to the fact that transferred trackebacks are basically "broken" and lead to different junitxml output

  6. emin63 commented on Jul 15, 2017

    @emin63
    Author

    Just an update for anyone coming here to parse pytest output:

    1. pytest-json does not parse errors in trying to load tests/modules (see no mention of errors in trying to load modules mattcl/pytest-json#3)
    2. I've had the best luck using junitxml with the xmltodict module. If you really want json, you could just dump the result to json using json.dumps.

    Thanks.

  7. kaczmarj commented on Jan 22, 2021

    @kaczmarj

    i am wondering how to parse pytest output as well. https://github.com/pytest-dev/pytest-reportlog seems to be the right tool for me.

  8. jeffwright13 commented on Dec 19, 2022

    @jeffwright13

    All of the solutions speak of tools that insert themselves in the middle of a test run as it executes - not after the fact. So if all you have is the console output, and you don't have access to the machine that's running the pytest job - like, in an environment where everything is containerized and shuttled off to a Jenkins job to run - seems like you're basically screwed.

  9. RonnyPfannschmidt commented on Dec 20, 2022

    @RonnyPfannschmidt
    Member

    exactly, as intended - if youwant parsable output, make sure you get an artifact for that

  10. RonnyPfannschmidt commented on Dec 20, 2022

    @RonnyPfannschmidt
    Member

    that being said, it might be interesting to have normal ux on stderr and reportlog on stdout

  11. nimdanor commented on Nov 22, 2025

    @nimdanor

    yes it would be a good thing to be able to output a machine readable format by using a simple cli option. Like
    python -m pytest -h whatever

  12. jeffwright13 commented on Nov 22, 2025

    @jeffwright13

    yes it would be a good thing to be able to output a machine readable format by using a simple cli option. Like python -m pytest -h whatever

    I wrote a plugin a while back that addresses some shortcomings I saw in the existing solutions. It can produce JSON output as per the specified format. The command-line to do so would be

    pytest --recap
    

    There is also a discussion on that repo's README that talks about the various options (that I knew of at the time) for outputting machine-readable session files.

  13. nicoddemus commented on Nov 22, 2025

    @nicoddemus
    Member
  14. nimdanor commented on Nov 22, 2025

    @nimdanor

    thanks i will use reportlog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions