Junior — Middle
Can you explain what is meant by fixtures in the context of automated testing?
sobes.tech AI
Answer from AI
In automated testing, fixtures are pre-prepared conditions or objects necessary for executing tests. They create a stable and predictable environment so that tests are reproducible and independent.
Fixtures can include:
- data preparation (e.g., filling a database with test records),
- environment setup,
- creation of objects or configurations,
- cleanup after tests.
In Python, for example, in the pytest framework, fixtures are implemented as functions decorated with @pytest.fixture, which can be used in tests for resource setup and teardown.
Example of a fixture in pytest:
import pytest
@pytest.fixture
def sample_list():
return [1, 2, 3]
def test_sum(sample_list):
assert sum(sample_list) == 6
Here, sample_list is a fixture that provides a ready-made list for the test.