Sobes.tech
Junior — Middle

How did you connect and use fixtures in your tests?

sobes.tech AI

Answer from AI

In Python, fixtures are commonly used in the pytest testing framework for preparing and cleaning up test data or environment.

To use a fixture, it is declared with the @pytest.fixture decorator. Then, in tests, it can be used by specifying the fixture name as a parameter to the test function — pytest will automatically call the fixture and pass the result.

Example:

import pytest

@pytest.fixture
def sample_list():
    # Data preparation
    return [1, 2, 3, 4]


def test_sum(sample_list):
    assert sum(sample_list) == 10

In this example, the sample_list fixture creates a list that is then used in the test_sum test.

Fixtures can have a scope, such as function, class, module, or session, which allows managing the lifetime of resources.

Fixtures can also be used for setting up databases, mocks, configurations, etc.