1

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?

Dane Brouwer
  • 2,827
  • 1
  • 22
  • 30
JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • Does this answer your question? [What does the "yield" keyword do?](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do) – shaik moeed Feb 27 '20 at 07:22

1 Answers1

2

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.

ChantOfSpirit
  • 157
  • 1
  • 1
  • 9