相关疑难解决方法(0)

在pytest中,我如何判断测试是否失败?(来自"请求")

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

有没有办法可以做到这一点?对于这个(或者我找不到),文档很安静.我会认为它会是这样的

request.function.failed
Run Code Online (Sandbox Code Playgroud)

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

这就是我想要做的:

@pytest.fixture()
def something(request):
    if request.function.failed:
        print "I failed"
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

python testing selenium pytest

13
推荐指数
2
解决办法
5153
查看次数

无法在装饰器中捕获 pytest 的结果

我的 pytest 测试装饰器在调用函数后立即退出装饰器。如果我使用 python 而不是 pytest 运行该文件,效果会很好。

这是代码:

def dec(func):
    def wrapper(*args, **kwargs):
        print('do some stuff')
        result = func(*args, **kwargs)
        print('ran function')
        return False if result else return True
    return wrapper

@dec
def test_something_is_not_true():
    return False # assert False

@dec
def test_something_is_true():
    return True # assert True


print(test_something_is_not_true())
print(test_something_is_true())
Run Code Online (Sandbox Code Playgroud)

当我用 python 运行该文件时,结果如下:

C:\tests> python test.py
do some stuff
ran function
True
do some stuff
ran function
False
Run Code Online (Sandbox Code Playgroud)

效果很好!

但是当我用 pytest 运行它时,它停在该result = func(*args, **kwargs)行并且从不执​​行该print('ran function')行:

C:\tests>pytest …
Run Code Online (Sandbox Code Playgroud)

python pytest python-decorators

2
推荐指数
1
解决办法
1990
查看次数

标签 统计

pytest ×2

python ×2

python-decorators ×1

selenium ×1

testing ×1