Meaning of yield within fixture in pytest

回眸只為那壹抹淺笑 提交于 2020-03-03 13:59:48

问题


I read the documentation at docs.pytest.org
I'm not sure about the meaning of the statement: yield smtp_connection

Can please someone explain what yield does, and if it's mandatory?


回答1:


First of all it's not mandatory!!! Yield execute test body, for example, you can set up your test with pre-condition and post condition. For this thing we can use 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")

Our test, for example store in test.py:

def test(set_up_pre_and_post_conditions):
    print("Body of test")

So, let's launch it: pytest test.py -v -s Output:

test.py::test Pre condition
Body of test
PASSEDPost condition

It's not full functionality of yield, just example, I hope it will be helpful.



来源:https://stackoverflow.com/questions/60428083/meaning-of-yield-within-fixture-in-pytest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!