[pytest-dev] parametrize and ids

Brianna Laugher brianna.laugher at gmail.com
Wed May 29 04:16:55 CEST 2013


Hi,

I was wondering if anyone has ideas about 'nicer' ways to specify IDs for
tests when using the parametrize mark.

Example:

my parametrize might look like this:

@py.test.mark.parametrize(('wx', 'expectedCoverage', 'expectedTrend'), [
    (['ra2', 'ra2', 'ra4', 'ra4'], 'Isol', None),
    (['sh2', 'sh2', 'sh4', 'sh4'], 'Isol', 'increasing'),
    (['ts2', 'ts2', 'ts4', 'ts4'], 'Isol', None),
    (['ra2', 'ra2', 'ra2', None], 'Isol', None),
    (['sh2', 'sh2', 'sh2', None], 'Isol', 'clearing'),
    (['ts2', 'ts2', 'ts2', None], 'Isol', None),
    ([None, None, 'ra2', 'ra2'], 'Isol', None),
    ([None, None, 'sh2', 'sh2'], 'Isol', 'developing'),
    ([None, None, 'ts2', 'ts2'], 'Isol', None),
])

which results in IDs like this:

  <Function 'test_noTrendsStartingWithChc[.30-Isol-increasing]'>
  <Function 'test_noTrendsStartingWithChc[.33-Isol-.35]'>
  <Function 'test_noTrendsStartingWithChc[.36-Isol-.38]'>
  <Function 'test_noTrendsStartingWithChc[.39-Isol-clearing]'>
  <Function 'test_noTrendsStartingWithChc[.42-Isol-.44]'>
  <Function 'test_noTrendsStartingWithChc[.45-Isol-.47]'>
  <Function 'test_noTrendsStartingWithChc[.48-Isol-developing]'>
  <Function 'test_noTrendsStartingWithChc[.51-Isol-.53]'>

I can add IDs (I just learned last week! :)) like this:

@py.test.mark.parametrize(('wx', 'expectedCoverage', 'expectedTrend'), [
    (['ra2', 'ra2', 'ra4', 'ra4'], 'Isol', None),
    (['sh2', 'sh2', 'sh4', 'sh4'], 'Isol', 'increasing'),
    (['ts2', 'ts2', 'ts4', 'ts4'], 'Isol', None),
    (['ra2', 'ra2', 'ra2', None], 'Isol', None),
    (['sh2', 'sh2', 'sh2', None], 'Isol', 'clearing'),
    (['ts2', 'ts2', 'ts2', None], 'Isol', None),
    ([None, None, 'ra2', 'ra2'], 'Isol', None),
    ([None, None, 'sh2', 'sh2'], 'Isol', 'developing'),
    ([None, None, 'ts2', 'ts2'], 'Isol', None),
], ids=['IsolRA increasing',
        'IsolSH increasing',
        'IsolTS increasing',
        'IsolRA clearing',
        'IsolSH clearing',
        'IsolTS clearing',
        'IsolRA developing',
        'IsolSH developing',
        'IsolTS developing',
])

but there are a couple of things I don't like about this:
* takes a lot of lines
* ID is separate from the test data, bad for maintenance, if the test data
gets changed the test name could easily be overlooked when it should be
updated too

What I would really like is if a string in the tuples could represent the
name. Maybe there could be a "magic" fieldname (id, testid, _pytestid ?)
that could be stripped out from the data and used as the id.

e.g.

@py.test.mark.parametrize(('id', 'wx', 'expectedCoverage',
'expectedTrend'), [
    ('ChcRA increasing', ['ra1', 'ra1', 'ra4', 'ra4'], 'Wide', None),
    ('ChcSH increasing', ['sh1', 'sh1', 'sh4', 'sh4'], 'Wide', None),
    ('ChcTS increasing', ['ts1', 'ts1', 'ts4', 'ts4'], 'Wide', None),
])
def test_noTrendsStartingWithChc(wx, expectedCoverage, expectedTrend):
    # blah

This would be really nice.
Another alternative could be if you could specify a function that would
take the test data and form a string for the test id from it. An example
here might be


@py.test.mark.parametrize(('wx', 'expectedCoverage', 'expectedTrend'), [
    (['ra1', 'ra1', 'ra4', 'ra4'], 'Wide', None),
    (['sh1', 'sh1', 'sh4', 'sh4'], 'Wide', None),
    (['ts1', 'ts1', 'ts4', 'ts4'], 'Wide', None),
], idFn=lambda (wx, cov, trend): '{!s}{} {!s}'.format(wx[0], cov, trend))

However this is quite a bit more messy, and not as flexible as specifying
your own string.

I would be interested to hear what people think about this, as well as if
there are other ways of specifying ids that I have overlooked. (I know I
could lay out the data the way I want and use pytest_generate_tests to
slice it up for metafunc.parametrize, but I actually prefer explicitly
writing out each test case as it tends to be a lot simpler for other
developers and future-me to grok.)

thanks,
Brianna


-- 
They've just been waiting in a mountain for the right moment:
http://modernthings.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20130529/55a92070/attachment-0001.html>


More information about the Pytest-dev mailing list