The difference between a fixture and an exit in a drink

I go through pytest fixtures and the following looks pretty similar, recent works are very similar.

Yes, readability is better in yield_fixure , but someone can tell me exactly what the difference is.

which should i use in the cases mentioned below?

@pytest.fixture()
def open_browser(request):
    print("Browser opened")

    def close_browser():
        print("browser closed")

    request.addfinalizer(close_browser)

    return "browser object"

@pytest.yield_fixture()
def open_browser():
    print("Browser opened")
    yield "browser object"
    print("browser closed")


def test_google_search(open_browser):
    print(open_browser)
    print("test_google_search")
+4
source share
1 answer

The only difference in readability. I think (although I'm not 100% sure) the basic behavior is identical (i.e., cleaning after the statement yieldis done as a finalizer). I always prefer to use cleaning tools, as this is more readable.

pytest < 3.0, pytest.yield_fixture . pytest 3.0+, pytest.yield_fixture , pytest.fixture yield_fixture.

:

pytest-3.0, , , yield, , , yield_fixture .

yield_fixture - , .

+6

Source: https://habr.com/ru/post/1683886/


All Articles