2

I have a pytest fixture defined in conftest.py that looks like this:

@pytest.fixture(scope="function")
def setup_multiple(create, delete):
    @contextlib.contextmanager
    def f(type, num=1):
        names = []
        try:
            for i in range(num):
                name = type + str(i + 1)
                names.append(name)
                create(name)

            yield names
        finally:
            for name in names:
                delete(name)

    return f

The fixture is widely used in numerous modules but there is one that I'm working on that requires using the same items generated by the fixture in each test, basically changing the scope of the fixture to module. I've been looking for a solution for several days but I couldn't find anything that would work in my case. Using yield from doesn't seem to work when you're trying to "increase" the scope to a larger one, only the other way around. Is there any way of doing this without rewriting the whole fixture, just with a different scope? I can't change the scope in the existing fixture since several other tests use it in it's current form.

0

Browse other questions tagged or ask your own question.