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

Send parameters in parametrize as dict to ids= callable #3628

Closed
wstrm opened this issue Jun 28, 2018 · 3 comments
Closed

Send parameters in parametrize as dict to ids= callable #3628

wstrm opened this issue Jun 28, 2018 · 3 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@wstrm
Copy link

wstrm commented Jun 28, 2018

Hello,

Is there a way to get all the parameters for parametrize inside a dict and sent to a ids= callable?

I would like to do something like:

@pytest.mark.parametrize("a, b, c",
[
  (1518, 200, 3600),
  (67, 10, 3600)
],
ids=(lambda p: 'Frame size: {a}B, Rate: {b}Mbit/s, Duration: {c}s'.format(**p)))
def test_case_one(a, b, c):
...

So I'd get the output:

test_case_one[Frame size: 1518B, Rate: 200Mbit/s, Duration: 3600s] FAILED
...
test_case_one[Frame size: 67B, Rate: 10Mbit/s, Duration: 3600s] PASSED
...

But as the ids= callable is called for every value it's hard to identify what each value is when they're integers.

One solution would be to use only one dict parameter, but then I would need to rewrite lot of tests...

Thanks :)

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #2272 (Question: Can callable as argument to ids parameter of parametrize of operate on a tuple?), #2176 (parametrize ignores marks if parameters are symbols), #1104 (UnicodeEncodeError with unicode parameters in mark.parametrize), #570 (indirect=True in parametrize breaks fixture scopes), and #2853 ("parametrize" misspelled).

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Jun 28, 2018
@ikonst
Copy link
Contributor

ikonst commented Jul 5, 2018

@willeponken consider adding a wrapper for parametrize, e.g.

def my_parametrize(argname, argvalues, indirect=False, ids=None, scope=None):
    if not ids:
        argnames = argname.split(',') if isinstance(argname, str) else argname
        ids = [
            ','.join(f'{k}={v}' for k, v in zip(argnames, p_argvalues))
            for p_argvalues in argvalues
        ]
    return pytest.mark.parametrize(argname, argvalues,
                                   indirect=indirect,
                                   ids=ids,
                                   scope=scope)

@my_parametrize('a,b,c', [(1, 2, 3), (4, 5, 6), (7, 8, 9)])
def test_foo(a, b, c):
    assert False

This will produce test IDs like test_foo[a=1,b=2,c=3].

@wstrm
Copy link
Author

wstrm commented Jul 5, 2018

Aha! Exactly what I wanted, thank you 👍

@wstrm wstrm closed this as completed Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants