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 />Answer1:
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.Answer2:
pytest-html and then run test with