Collecting and Reporting pytest Results


I am doing some Selenium testing via pytest. The next step is to start doing some reporting. I'd like to write something that allows me to run the tests, collect the results and send out an email. So far the closest thing that I've found to this is writing out the test result to the result log and then use a plugin to check the exist status and send an email from there. This works but is slightly cumbersome and I'm hoping that there is a more elegant way to do it. While the overall pytest documentation is great, the plugin documentation is rather poor - I can't even find pytest_sessionfinish anywhere, even though it appears to work.

import pytest class MyPlugin: def pytest_sessionfinish(self, exitstatus): if exitstatus == 0: #Send success email pass else: #Read output.txt #Add output.txt to email body #Send email pass pytest.main("--resultlog=output.txt", plugins=[MyPlugin()]) <hr />

<strong>Q: What is the best way to run and collect results from pytest?</strong>

<hr />


One easy way to generate a result report is using the pytest option --junitxml when running tests. pytest will generate a test report in JUnit format.

Since JUnit is widely used, it's easy to find tools to parse the report and generate some good looking output, like HTML reports. As far as I know, there are some plugins on Jenkins that work fine for parsing JUnit reports and provide nice reports.

Visit <a href="https://pytest.org/latest/usage.html" rel="nofollow">https://pytest.org/latest/usage.html</a> and refer 'Creating JUnitXML format files' part.

More than that, pytest provides a way to expand the JUnit XML report when you have access to the pytest object request or config:

if hasattr(request.config, "_xml"): request.config._xml.add_custom_property(name, value)

If in test cases, pytest provides a fixture to do that:

def test_function(record_xml_property): record_xml_property("key", "value") assert 0

This will add a custom property to the JUnit XML report.


Install pytest-html and then run test with --html=pytest_report.html option.


  • How can I pass a list of values as a parameter to a Visual Studio web performance test or load test
  • How to get a slug name for a page without creating an admin page menu
  • javascript - change the Page source Reference File Path
  • Rails- Heroku - rake aborted! Please install the postgresql adapter: gem install activerecord-postgr
  • Are there any supported high bit-depth video or image formats in DirectShow
  • Google Places API - Get more than 10 photos from Details Response?
  • Pytest/Allure - How to generate testcase description?
  • Equivalent of branch..merge for git-push
  • What is the right syntax for “timeframe” in MailChimp API 3.0
  • How to get the sql state from libpq?
  • Does SmartGit support git-svn?
  • How to force Composer to download a local package?
  • end daemon processes with multiprocessing module
  • PHP Listener Script for Paypal Webhooks
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • Iron Router: How do I send data to the layout?
  • How Get arguments value using inline assembly in C without Glibc?
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • How to autopopulate a field in SugarCRM form
  • Clear activity stack before launching another activity
  • Differences in dis-assembled C code of GCC and Borland?
  • Android application: how to use the camera and grab the image bytes?
  • Create DicomImage from scratch using Dcmtk
  • Get one-time binding to work for ng-if
  • Retrieving value from sql ExecuteScalar()
  • Is there any way to access browser form field suggestions from JavaScript?
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Java applet as stand-alone Windows application?
  • Do create extension work in single-user mode in postgres?
  • Jquery - Jquery Wysiwyg return html as a string
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Python: how to group similar lists together in a list of lists?
  • SQL merge duplicate rows and join values that are different
  • Busy indicator not showing up in wpf window [duplicate]
  • Reading document lines to the user (python)
  • How to load view controller without button in storyboard?