Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/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 3.x pytest中夹具内屈服的含义_Python 3.x_Pytest - Fatal编程技术网

Python 3.x pytest中夹具内屈服的含义

Python 3.x pytest中夹具内屈服的含义,python-3.x,pytest,Python 3.x,Pytest,我在 我不确定该语句的含义:yield smtp\u connection 请有人解释一下,收益率的作用,以及它是否是强制性的吗?首先,它不是强制性的!!! 例如,您可以使用前置条件和后置条件设置测试。为此,我们可以使用conftest.py: import pytest @pytest.fixture def set_up_pre_and_post_conditions(): print("Pre condition") yield # this will be execu

我在
我不确定该语句的含义:
yield smtp\u connection


请有人解释一下,
收益率
的作用,以及它是否是强制性的吗?

首先,它不是强制性的!!! 例如,您可以使用前置条件和后置条件设置测试。为此,我们可以使用conftest.py:

import pytest


@pytest.fixture
def set_up_pre_and_post_conditions():
    print("Pre condition")
    yield # this will be executed our test
    print("Post condition")
我们的测试,例如存储在test.py中:

def test(set_up_pre_and_post_conditions):
    print("Body of test")
那么,让我们启动它:pytest.py-v-s 输出:


这不是yield的全部功能,只是一个例子,我希望它会有所帮助。

这是否回答了您的问题?
test.py::test Pre condition
Body of test
PASSEDPost condition