Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

How create parametrize test in pytest, if parameter is pytest.fixture?

  1. User

    How create parametrize test in pytest, if parameter is pytest.fixture?

  2. Assistant

    To create a parameterized test in pytest with a fixture as a parameter, you can use the pytest.mark.parametrize decorator along with the fixture name as one of the parameters. Here's an example:

    import pytest
    
    @pytest.fixture
    def my_fixture():
        return ["apple", "banana", "orange"]
    
    @pytest.mark.parametrize("input_value", [1, 2, 3])
    def test_my_test(input_value, my_fixture):
        fruit = my_fixture[input_value-1]
        assert len(fruit) > 0
    

    In the above example, we have a fixture called my_fixture that returns a list of fruits. We then use pytest.mark.parametrize to create a parameterized test where input_value is one of the parameters and is set to a list of values [1, 2, 3]. We also include my_fixture as a parameter in the test function. Inside the test function, we use my_fixture to get a fruit based on the input_value parameter and assert that the fruit has a non-zero length.

    When you run this test, pytest will execute the test three times, once for each of the input_value values (1, 2, 3), with the my_fixture fixture providing the list of fruits in each iteration.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.