Middle
What annotations have you used in pytest?
sobes.tech AI
Answer from AI
In pytest, I often used the following annotations (decorators):
@pytest.mark.parametrize— for parameterizing tests to run a test with different data sets.@pytest.fixture— for creating and managing fixtures that prepare the test environment.@pytest.mark.skipand@pytest.mark.skipif— for skipping tests or conditionally skipping.@pytest.mark.xfail— for marking tests that are expected to fail.
Example of parameterization:
import pytest
@pytest.mark.parametrize("input,expected", [
(2, 4),
(3, 9),
(4, 16),
])
def test_square(input, expected):
assert input * input == expected
Fixture for preparing data:
import pytest
@pytest.fixture
def sample_data():
return {'key': 'value'}
def test_using_fixture(sample_data):
assert sample_data['key'] == 'value'