30. Fixtures in pytest
Fixtures provide reusable setup and teardown logic for tests.
Example Fixture
import pytest
@pytest.fixture
def sample_list():
return [1, 2, 3]
def test_sum(sample_list):
assert sum(sample_list) == 6
Why Use Fixtures?
- Avoid duplication
- Provide consistent setup across tests
Wrap-Up
Fixtures make pytest more powerful by simplifying test setup.