Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex based fixture names for parameter inclusion #5581

Open
RonnyPfannschmidt opened this issue Jul 9, 2019 · 1 comment
Open

regex based fixture names for parameter inclusion #5581

RonnyPfannschmidt opened this issue Jul 9, 2019 · 1 comment
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@RonnyPfannschmidt
Copy link
Member

RonnyPfannschmidt commented Jul 9, 2019

follow-up to #5571

@pytest.fixture(name=re.compile("user_(?P<role>user|mod|admin)")
def system_user(request):
   role = get_role_by(name=request.match.group("role"))
   return create_user(..., role=role)
@RonnyPfannschmidt RonnyPfannschmidt added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature topic: parametrize related to @pytest.mark.parametrize topic: fixtures anything involving fixtures directly or indirectly labels Jul 9, 2019
@nicoddemus
Copy link
Member

Hmm my initial gut feeling for this is -1, because:

  1. pytest fixtures are already considered "magical" and implicit. This makes it even more so, because now we don't have an explicit def user_admin, def user_mod, etc, to search for the fixture definition.

  2. We already have a way to express the above by making fixtures that return a factory:

    @pytest.fixture
    def user_factory():
        def fac(role):
            role = get_role_by(name=request.match.group("role"))
            return create_user(..., role=role) 
        return fac
        
    
    def test_foo(user_factory):
        admin = user_factory('admin')  

    This is a little more verbose, but I think it being more explicit overweights that.

The idea proposed here is to basically provide a way to pass a parameter to a fixture from inside a test, which I'm not against per-se; but using a regex approach doesn't really strikes me as a good way to accomplish that.

We might consider adding another way to do the same thing with a special decorator:

@pytest.fixture_factory(rule=None)
def system_user(request):
    role = get_role_by(name=request.getparam("role"))
    return create_user(..., role=role)

def test_foo(system_user):
   admin = system_user('admin')  # other fixtures calling this 
                                 # will also receive the same instance

But I'm not too sure either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

2 participants