[Python] aggregate or split pytest fixtures

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    Two related pytest questions:

    Split fixture into individual values


    I have a fixture which is an iterable and which is tested in a certain test. This works. However, I also have a test which wants to test the individual values - I don't know how to do this.

    For example, how can I write the student fixture such that it returns each of the elements of klass?

    @pytest.fixture
    def klass() -> list[str]:
    return ['alice', 'bob', 'claire']

    @pytest.fixture
    def student(klass) -> str: # HERE IS THE QUESTION
    #code that returns each student in the klass individually


    def test_klass(klass): # ran once
    assert len(klass) > 0

    def test_student(student): # ran 3 times
    assert isinstance(student, str)

    Combine individual fixture values into single fixture


    The inverse problem occurs if I want to specify the students and combine them into a klass (the same test functions apply as above):

    @pytest.fixture(params = ['alice', 'bob', 'claire'])
    def student(request) -> str:
    return request.param

    @pytest.fixture
    def klass(student) -> list[str]: # HERE IS THE QUESTION
    #code that returns the list with all values for student

    Continue reading...

Compartilhe esta Página