difference between fixture and yield_fixture in pytest

The only difference is in readability. I think (though I’m not 100% sure) the underlying behavior is identical (i.e. the cleanup after the yield statement is run as a finalizer). I always prefer using yield fixtures for cleanup, since it’s more readable.

If you’re using pytest <3.0, you’ll still need to use pytest.yield_fixture to get that behavior. But if you’re able to use pytest 3.0+, pytest.yield_fixture is deprecated and you can use pytest.fixture to get the same yield_fixture behavior.

Here are the explanatory docs:

Since pytest-3.0, fixtures using the normal fixture decorator can use
a yield statement to provide fixture values and execute teardown code,
exactly like yield_fixture in previous versions.

Marking functions as yield_fixture is still supported, but deprecated
and should not be used in new code.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)