Skip to content

Reuse a fixture with parameters from another fixture #13658

@Wovchena

Description

@Wovchena

Downloading images from the Internet is flaky. I'm trying to reuse the images in tests to reduce instability. I also want the images to be removed from memory after they aren't needed. I'm trying to achieve that with the following code

import pytest

@pytest.fixture(scope="module")
def image(request):
    print(f"image-{request.param}")
    return f"image-{request.param}"

@pytest.fixture(scope="module")
def images(request):
    print(f"images{request.param}")
    values = []
    for val in request.param:
        request._pyfuncitem.callspec.params["image"] = val
        request._pyfuncitem.callspec.indices["image"] = 0
        request._pyfuncitem.callspec._arg2scope["image"] = request._pyfuncitem.callspec._arg2scope["images"]
        values.append(request.getfixturevalue('image'))
    return "images[" + ' '.join(values) + "]"

@pytest.mark.parametrize("image", [1, 2], indirect=True)
def test_image(image):
    print(f"test_image: {image}")

@pytest.mark.parametrize("images", [[], [1], [1, 2, 3]], indirect=True)
def test_images(images):
    print(f"test_images: {images}")

test_images[images2] prints test_images: images[image-1 image-1 image-1] instead of 1, 2, 3. I'd like request.getfixturevalue() to have a parameter to be passed to the fixture. I've checked pytest-lazy-fixture but its fixtures cannot be parametrized (https://stackoverflow.com/a/62166129/25242963).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions