在 pytest 中,如何判断测试是否失败?(来自“请求") [英] In pytest, how can I figure out if a test failed? (from "request")

查看:113
本文介绍了在 pytest 中,如何判断测试是否失败?(来自“请求")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium 和 PYTEST 来测试站点.我想在测试失败时(并且仅在失败时)截取页面的屏幕截图.

I'm using Selenium with PYTEST to test a site. I would like to take a screenshot of the page whenever a test fails (and only when it fails).

有没有办法做到这一点?当涉及到这个时,文档很安静(或者我找不到它).我认为它会像

Is there a way that I can do this? The docs are quiet when it comes to this (or I can't find it). I would assume that it would be something like

request.function.failed

它会返回一个布尔值或其他东西.

and it would return a boolean or something.

这就是我想做的:

@pytest.fixture()
def something(request):
    if request.function.failed:
        print "I failed"

当然,这将被添加到终结器中.可以做到吗?使用 pytest 2.3.3

This would be added to a finalizer, of course. Can it be done? Using pytest 2.3.3

谢谢.

推荐答案

它可以完成,但不能直接完成.我只是 添加了一个文档示例.默认情况下使这更容易可能是有意义的,即不需要使用 conftest.py 钩子.如果您同意,请提交问题.

It can be done, not directly though. I just added an example to the docs. It probably makes sense to makes this easier by default, i.e. without requiring the use of a conftest.py hook. If you agree, please file an issue.

这篇关于在 pytest 中,如何判断测试是否失败?(来自“请求")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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