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

Question: Can callable as argument to ids parameter of parametrize of operate on a tuple? #2272

Closed
vreuter opened this issue Feb 22, 2017 · 3 comments

Comments

@vreuter
Copy link

vreuter commented Feb 22, 2017

Using Python 2.7.12 and pytest 3.0.6

Sometimes I parameterize test cases with arguments via argvalues=itertools.product(..., ..., etc.). I'd like to be able to label the parameterized test cases but treat each argument a bit differently based on type. From the parametrize docstring, I was a bit confused whether the intent was to have the callable operate on a single element of an argval tuple, or the entire tuple. Just anecdotally, it seems like the ids customization is operating on one element of the argument value tuple rather than on the entire tuple, prohibiting the position-(type-)dependent behavior that I'd like when using a callable as the argument to ids. I can work around this by putting some type-indicative conditional logic in the callable that I pass to operate on a single tuple element, or just build up a list of string ids instead, but I'd like a cleaner callable route if possible. I'm wondering if I'm making a mistake in how I'm using this feature, or whether this is something that isn't currently supported.

import itertools
import pytest


def df1():
    return

def df2():
    return


@pytest.mark.parametrize(argnames="func,text",
                         argvalues=itertools.product([df1, df2],
                                                     ["first", "second"]),
                         ids=lambda args: "{}:{}".format(args[0].__name__,
                                                         args[1]))
def test_ids(func, text):
    pass
@vreuter vreuter changed the title Question: Can callable as argument to ids parameter of operate on a tuple? Question: Can callable as argument to ids parameter of parametrize of operate on a tuple? Feb 22, 2017
@RonnyPfannschmidt
Copy link
Member

are you aware that multiple parametrize marks do the product on their own?

@vreuter
Copy link
Author

vreuter commented Feb 22, 2017

Ah, excellent! Hadn't tried that, thanks.

@vreuter vreuter closed this as completed Feb 22, 2017
@RonnyPfannschmidt
Copy link
Member

i.e.

import itertools
import pytest


def df1():
    return

def df2():
    return


@pytest.mark.parametrize('func', [df1, df2])
@pytest.mark.parametrize('text', ["first", "second"])
def test_ids(func, text):
    pass

if i recall correct, py.test even manages the names correctly then on its own

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants