Junior — Middle
Could you give an example of using a fixture in testing?
sobes.tech AI
Answer from AI
A fixture in testing is a prepared state or set of data used to run tests. For example, in automated tests in Python using pytest, a fixture can create a database connection or prepare test data before executing a test:
import pytest
@pytest.fixture
def sample_data():
return {'user': 'test_user', 'password': '12345'}
def test_login(sample_data):
assert sample_data['user'] == 'test_user'
Here, the sample_data fixture provides test data that is then used in the test_login test. This helps avoid code duplication and simplifies test maintenance.