Skip to content

Add a get_marker method to metafunc #1425

Closed

Description

@birdsarah

I would like to use a marker to specify if I want a test parametrized. At the moment, I have to do the following:

def pytest_generate_tests(metafunc):
    if hasattr(metafunc.function, "cross_browser"):
         metafunc.parametrize('the fixture', params_list)

I was expecting an interface like item: item.get_marker('cross_browser') and it took me a while to hunt down this solution.

It seems like it would be a fairly straight forward addition, and I'd be willing to try rustling up a PR. Is there a reason that this isn't a feature already or is it just a matter of someone doing it?

Activity

  1. hpk42 commented on Mar 2, 2016

    @hpk42
    Contributor

    indeed, a metafunc.get_marker() looks like a very useful addition. Please go for a PR against the "features" branch :)

  2. RonnyPfannschmidt commented on Mar 2, 2016

    @RonnyPfannschmidt
    Member

    Btw, Imho we should Phase out metafunc in 3.x, its a artifact of unfortunate structure

  3. hpk42 commented on Mar 2, 2016

    @hpk42
    Contributor

    ronny, i see that as an orthogonal issue and one which i don't understand in detail. Maybe you can take it to the pytest-dev ML sometime to detail your reasoning behind the suggestion.

  4. RonnyPfannschmidt commented on Mar 2, 2016

    @RonnyPfannschmidt
    Member

    Will do at the office

  5. RonnyPfannschmidt commented on Jun 23, 2017

    @RonnyPfannschmidt
    Member

    closing this one as superseeded

  6. ThomasWaldmann commented on Nov 15, 2018

    @ThomasWaldmann
    def pytest_generate_tests(metafunc):
        argnames = metafunc.fixturenames
    
        if 'store' in argnames:
            klasses = 'BytesStore', 'FileStore'
            argname = 'store'
        elif 'bst' in argnames:
            klasses = 'BytesStore',
            argname = 'bst'
        elif 'fst' in argnames:
            klasses = 'FileStore',
            argname = 'fst'
        else:
            klasses = None
            argname = None
    
        if klasses is not None:
            ids = []
            argvalues = []
            for storename in STORES:
                for klass in klasses:
                    ids.append('{0}/{1}'.format(storename, klass))
                    argvalues.append((storename, klass))
            metafunc.parametrize(argname, argvalues, ids=ids, indirect=True)
    
        multi_mark = getattr(metafunc.function, 'multi', None)
        mm = metafunc.function.get_closest_marker('multi')
        if multi_mark is not None:
            # XXX: hack
            ids = []
            argvalues = []
            stores = multi_mark.kwargs['Store']    # XXX blows up on pytest4
            for store in stores:
                ids.append(store.__name__)
                argvalues.append(store)
            metafunc.parametrize('Store', argvalues, ids=ids)
    

    So, how can I access markers "the new way" from this function?

    I read some docs, but that is still totally unclear to me and it seems that I also miss a get_marker function on the metafunc.

  7. RonnyPfannschmidt commented on Nov 15, 2018

    @RonnyPfannschmidt
    Member

    @ThomasWaldmann metafunc.node.iter_markers

  8. ThomasWaldmann commented on Nov 15, 2018

    @ThomasWaldmann
    E   AttributeError: 'Metafunc' object has no attribute 'node'
    
  9. RonnyPfannschmidt commented on Nov 15, 2018

    @RonnyPfannschmidt
    Member

    i'm sorry, i did miss remember, based on

    self.definition = definition
    its definition not node

  10. ThomasWaldmann commented on Nov 15, 2018

    @ThomasWaldmann

    Also undocumented, but works. I get the Mark instance now.

  11. RonnyPfannschmidt commented on Nov 15, 2018

    @RonnyPfannschmidt
    Member

    @ThomasWaldmann the plan was to introduce the FunctionDefinition node in the official collection tree since quite a while, but i failed 2 different structural attempts to integrate it already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: marksrelated to marks, either the general marks or builtin

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions