问题
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