Junior — Middle
What parameters can be passed to scope when using fixture in pytest?
sobes.tech AI
Answer from AI
In pytest, the scope parameter of fixtures determines the lifetime and scope of the fixture. The main values are:
function— the fixture is recreated for each test function (default).class— the fixture is created once per test class.module— the fixture is created once per module (test file).package— the fixture is created once per package (directory).session— the fixture is created once per test session.
Example:
import pytest
@pytest.fixture(scope='module')
def db_connection():
conn = create_connection()
yield conn
conn.close()
Here, the database connection will be created once for the entire module.