收集并报告pytest结果 [英] Collecting and Reporting pytest Results

查看:249
本文介绍了收集并报告pytest结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过pytest做一些硒测试.下一步是开始进行一些报告.我想写一些东西,让我可以运行测试,收集结果并发送电子邮件.到目前为止,我发现与此最接近的事情是将测试结果写到结果日志中,然后使用一个插件来检查存在状态并从那里发送电子邮件.这可行,但是有点麻烦,我希望有一种更优雅的方法.虽然pytest的总体文档不错,但是插件文档却很差-即使在任何地方pytest_sessionfinish似乎都可以用,我什至找不到pytest_sessionfinish.

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()])


问:从pytest运行并收集结果的最佳方法是什么?

推荐答案

一种生成结果报告的简单方法是在运行测试时使用pytest选项--junitxml. pytest将以JUnit格式生成测试报告.

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.

由于JUnit被广泛使用,因此很容易找到工具来分析报告并生成一些美观的输出,例如HTML报告.据我所知,Jenkins上有一些插件可以很好地解析JUnit报告并提供出色的报告.

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.

访问 https://pytest.org/latest/usage.html 并参阅创建JUnitXML格式文件的一部分.

Visit https://pytest.org/latest/usage.html and refer 'Creating JUnitXML format files' part.

除此之外,pytest提供了一种在您可以访问pytest对象请求或配置时扩展JUnit XML报表的方法:

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)

如果在测试用例中,pytest提供了一种解决方案:

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

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

这会将自定义属性添加到JUnit XML报表中.

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

这篇关于收集并报告pytest结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆