Junior — Middle
In what situations and at what moment do fixtures execute during testing?
sobes.tech AI
Answer from AI
Fixtures in testing are preparatory actions that create the necessary state for executing a test. They are performed before the test runs to ensure a stable and predictable environment. For example, a fixture might create test data, configure the environment, or initialize objects.
In most testing frameworks, fixtures are executed in methods with annotations like @Before or setup(), which are run before each test or before a set of tests. After the test, cleanup methods (e.g., @After) are often executed to restore the system to its original state.
Example in Python using pytest:
import pytest
@pytest.fixture
def setup_data():
data = {'user': 'test', 'password': '1234'}
return data
def test_login(setup_data):
assert setup_data['user'] == 'test'
Here, the fixture setup_data creates data that is used in the test.