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

Add a get_marker method to metafunc #1425

Closed
birdsarah opened this issue Mar 2, 2016 · 12 comments
Closed

Add a get_marker method to metafunc #1425

birdsarah opened this issue Mar 2, 2016 · 12 comments
Labels
topic: marks related to marks, either the general marks or builtin

Comments

@birdsarah
Copy link

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?

@hpk42
Copy link
Contributor

hpk42 commented Mar 2, 2016

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

@RonnyPfannschmidt
Copy link
Member

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

@hpk42
Copy link
Contributor

hpk42 commented Mar 2, 2016

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.

@RonnyPfannschmidt
Copy link
Member

Will do at the office

@RonnyPfannschmidt
Copy link
Member

closing this one as superseeded

@RonnyPfannschmidt RonnyPfannschmidt moved this from planning to discardable in resolve the mark fallout Jun 23, 2017
@RonnyPfannschmidt RonnyPfannschmidt removed this from discardable in resolve the mark fallout Jun 23, 2017
@ThomasWaldmann
Copy link

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.

@RonnyPfannschmidt
Copy link
Member

@ThomasWaldmann metafunc.node.iter_markers

@ThomasWaldmann
Copy link

@RonnyPfannschmidt thanks!

Is node a undocumented feature? https://docs.pytest.org/en/latest/reference.html#metafunc

@ThomasWaldmann
Copy link

E   AttributeError: 'Metafunc' object has no attribute 'node'

@RonnyPfannschmidt
Copy link
Member

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

self.definition = definition
its definition not node

@ThomasWaldmann
Copy link

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

@RonnyPfannschmidt
Copy link
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
Labels
topic: marks related to marks, either the general marks or builtin
Projects
None yet
Development

No branches or pull requests

4 participants