PyTest Fixture Scope
PyTest Fixture Scope
PyTest is a popular testing framework in Python that makes it easy to write simple and scalable test cases. One of its powerful features is fixtures, which help set up and clean up test environments efficiently. Fixtures provide a way to prepare test data, configure states, and clean up resources after tests have run.
In PyTest, you can control how often a fixture is created and used using the scope
parameter. By default, a fixture runs for each test function that requires it. However, PyTest allows changing the fixture scope to control how long the fixture instance remains active.
The available scopes are are as follows:
- Function.
- Class.
- Module.
- Session.
Scope: function (Default)
This is the default scope. The fixture runs once for each test function that uses it. If you need to perform an action before and after the test function of a module, you can use function scope.
import pytest
@pytest.fixture()
def sample_fixture():
print("Setup: Creating resource")
yield
print("Teardown: Cleaning up resource")
def test_case1(sample_fixture):
print("Executing Test Case 1")
def test_case2(sample_fixture):
print("Executing Test Case 2")
In this example, sample_fixture
is executed separately for test_case1
and test_case2
.
Scope: module
With scope="module"
, the fixture runs once per module and is shared across all test functions in that module. If you need to perform an action before and after of a module, you can use module scope (scope=“module”).
import pytest
@pytest.fixture(scope="module")
def module_fixture():
print("Setup: Module-level resource")
yield
print("Teardown: Cleaning up module resource")
def test_case1(module_fixture):
print("Executing Test Case 1")
def test_case2(module_fixture):
print("Executing Test Case 2")
The fixture runs once before any test case and is shared across tests in the module.
Scope: class
With scope="class"
, the fixture is executed once for each test class and shared among its test methods. If you need to perform an action before and after of a class of a module, you can use the class scope (scope=“class”).
import pytest
@pytest.fixture(scope="class")
def class_fixture():
print("Setup: Class-level resource")
yield
print("Teardown: Cleaning up class resource")
class TestExample:
def test_case1(self, class_fixture):
print("Executing Test Case 1")
def test_case2(self, class_fixture):
print("Executing Test Case 2")
The fixture runs once per test class and is reused in all methods of that class.
Scope: session
With scope="session"
, the fixture is created once per test session and shared across all tests. If you need to perform an action before and after for a set of methods in a folder or project, you use the session scope (scope=“session”). It creates single fixture for set of methods in a project or modules in the path.
import pytest
@pytest.fixture(scope="session")
def session_fixture():
print("Setup: Session-level resource")
yield
print("Teardown: Cleaning up session resource")
def test_case1(session_fixture):
print("Executing Test Case 1")
def test_case2(session_fixture):
print("Executing Test Case 2")
This fixture runs only once for the entire test session and is shared among all test cases.
PyTest fixtures with different scopes allow efficient test setup and resource management. Choosing the right scope ensures optimal performance and resource usage in test suites.