Skip to content

Instantly share code, notes, and snippets.

@pfctdayelise
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfctdayelise/86c8a577b60afb174178 to your computer and use it in GitHub Desktop.
Save pfctdayelise/86c8a577b60afb174178 to your computer and use it in GitHub Desktop.
import pytest
def params(n):
fn = range(n)
return {'name': 'a', 'args': fn, 'ids': ['a: {}'.format(i) for i in fn]}
named_args = params(3)
@pytest.mark.parametrize(
named_args['name'], named_args['args'], ids=named_args['ids'])
def test_funcs_with_naming_func(a):
assert a ** 3 == a * (a ** 2)
###############################################################################
## Our parametrize wrapper:
def myparametrize(argnames, argvalues):
def fmt(ns, vs):
return ', '.join('{}: {}'.format(n, v) for n, v in zip(ns, vs))
return pytest.mark.parametrize(argnames, argvalues,
ids=[fmt(argnames, vs) for vs in argvalues])
# As you can see, the single argument case looks a bit pathological,
# as pytest.mark.parametrize is doing extra work that we are no longer
# taking advantage of. But I assume this is not the usual case so it may not matter.
@myparametrize(('a',), [(0,), (1,), (2,)])
def test_func_with_myparametrize(a):
pass
@myparametrize(('a','b','c'), [(1, 2, 3), (10, 20, 30)])
def test_more_complicated_example(a, b, c):
pass
$ py.test testing/test_foo.py --collectonly
============================================================================================ test session starts ============================================================================================
platform linux2 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4
collected 8 items
<Module 'testing/test_foo.py'>
<Function 'test_funcs_with_naming_func[a: 0]'>
<Function 'test_funcs_with_naming_func[a: 1]'>
<Function 'test_funcs_with_naming_func[a: 2]'>
<Function 'test_func_with_myparametrize[a: 0]'>
<Function 'test_func_with_myparametrize[a: 1]'>
<Function 'test_func_with_myparametrize[a: 2]'>
<Function 'test_more_complicated_example[a: 1, b: 2, c: 3]'>
<Function 'test_more_complicated_example[a: 10, b: 20, c: 30]'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment