Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以在pytest中执行多个断言吗?_Python_Selenium_Pytest - Fatal编程技术网

Python 我可以在pytest中执行多个断言吗?

Python 我可以在pytest中执行多个断言吗?,python,selenium,pytest,Python,Selenium,Pytest,我正在使用pytest进行selenium测试,想知道在一个测试中是否可能有多个断言 我调用一个比较多个值的函数,我希望测试报告所有不匹配的值。我遇到的问题是,使用“assert”或“pytest.fail”会在发现不匹配的值时立即停止测试 有没有办法让测试继续运行并报告所有不匹配的值?正如Jon Clements评论的那样,您可以填写错误消息列表,然后断言列表为空,当断言为假时显示每条消息 具体来说,可能是这样的: def test_something(self): errors =

我正在使用pytest进行selenium测试,想知道在一个测试中是否可能有多个断言

我调用一个比较多个值的函数,我希望测试报告所有不匹配的值。我遇到的问题是,使用“assert”或“pytest.fail”会在发现不匹配的值时立即停止测试


有没有办法让测试继续运行并报告所有不匹配的值?

正如Jon Clements评论的那样,您可以填写错误消息列表,然后断言列表为空,当断言为假时显示每条消息

具体来说,可能是这样的:

def test_something(self):
    errors = []

    # replace assertions by conditions
    if not condition_1:
        errors.append("an error message")
    if not condition_2:
        errors.append("an other error message")

    # assert no error message has been registered, else print messages
    assert not errors, "errors occured:\n{}".format("\n".join(errors))
原始断言被
if
语句替换,这些语句在不满足条件的情况下将消息附加到
错误
列表中。 然后断言
错误
列表为空(空列表为假),并使断言消息包含
错误
列表的每条消息



您还可以制作一个测试生成器,如中所述。我没有找到任何描述它的pytest文档,但我知道pytest处理它的方式与nose完全相同。

这里有一种替代方法,它与@Tryph提供的方法非常相似,并提供更好的堆栈跟踪

PyPI上的包实现了这种方法。另请参阅GitHub上的存储库,或使用以下工具从PyPI安装:

pip install delayed-assert
您可以(可能)将任何断言库与python延迟断言结合使用。认为它更像是堆栈跟踪管理器库,而不是断言。检查示例使用情况

这就是错误堆栈跟踪的外观

是“一个pytest插件,允许每次测试多次失败”。下面是一个如何使用它的示例(取自
自述文件
):

即使某些断言失败,它们都会得到评估和报告:

======================================== FAILURES =========================================
_________________________________ test_simple_assume[1-1] _________________________________
>    pytest.assume(False)
test_assume.py:7

y          = 1
x          = 1
----------------------------------------
Failed Assumptions:1
_________________________________ test_simple_assume[1-0] _________________________________
>    pytest.assume(x == y)
test_assume.py:5

y          = 0
x          = 1
>    pytest.assume(False)
test_assume.py:7

y          = 0
x          = 1
----------------------------------------
Failed Assumptions:2
_________________________________ test_simple_assume[0-1] _________________________________
>    pytest.assume(x == y)
test_assume.py:5

y          = 1
x          = 0
>    pytest.assume(False)
test_assume.py:7

y          = 1
x          = 0
----------------------------------------
Failed Assumptions:2
================================ 3 failed in 0.02 seconds =================================

2017年关于pytest的实用书籍的作者Brian Okken提供了另一个图书馆。


这里有一个相当简单的方法:


导入pytest
def测试样本(文本):
flag=True
对于文本中的文本:
如果文本!=“任何东西”:
flag=False
如果标志==假:
pytest.fail(“文本不匹配”,pytrace=True)

难道你不能建立一个错误列表,然后断言该列表为空吗?不过我想报告单个错误,以便在报告中给出一个不匹配的值列表。。。如何建立一个非匹配列表,然后断言该列表为空(如果出现错误,则不会为空)-然后将该列表作为“预期的[]-得到的[1,2,7,10]`或者其他不需要的东西?谢谢,你知道我可以在哪里下载delayed_assert库吗?我一直在四处寻找,没有找到它。你不需要任何库。现在请使用此参考,它可以通过
pip install安装https://github.com/pr4bh4sh/python-delayed-assert
仅适用于前来参观的其他人在不同的插件中,
pytest-aspect
是从
pytest-expect
派生出来的。另外,请注意,
pytest-expect
pytest-check
的早期版本。都需要安装
pytest-aspect
pytest-check
======================================== FAILURES =========================================
_________________________________ test_simple_assume[1-1] _________________________________
>    pytest.assume(False)
test_assume.py:7

y          = 1
x          = 1
----------------------------------------
Failed Assumptions:1
_________________________________ test_simple_assume[1-0] _________________________________
>    pytest.assume(x == y)
test_assume.py:5

y          = 0
x          = 1
>    pytest.assume(False)
test_assume.py:7

y          = 0
x          = 1
----------------------------------------
Failed Assumptions:2
_________________________________ test_simple_assume[0-1] _________________________________
>    pytest.assume(x == y)
test_assume.py:5

y          = 1
x          = 0
>    pytest.assume(False)
test_assume.py:7

y          = 1
x          = 0
----------------------------------------
Failed Assumptions:2
================================ 3 failed in 0.02 seconds =================================
import pytest_check as check

def test_example():
    a = 1
    b = 2
    c = [2, 4, 6]
    check.greater(a, b)
    check.less_equal(b, a)
    check.is_in(a, c, "Is 1 in the list")
    check.is_not_in(b, c, "make sure 2 isn't in list")