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()])
Q: What is the best way to run and collect results from pytest?
To generate the report, we have to move from the current directory to the directory of the Pytest file that we want to execute. Then run the command: pytest --html=report. html. After this command is successfully executed, a new file called the report.
pytest. main() will return its exit code — which is an ExitCode enum, starting with pytest 5.0. 0. If a test has failed, ExitCode.
By setting the log_cli configuration option to true , pytest will output logging records as they are emitted directly into the console. You can specify the logging level for which log records with equal or higher level are printed to the console by passing --log-cli-level .
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 https://pytest.org/latest/usage.html 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With