[Pytest-commit] Issue #518: parametrize: allow ellipsis to avoid repeating argument list? (hpk42/pytest)
Matthias Geier
issues-reply at bitbucket.org
Sat May 24 16:52:25 CEST 2014
New issue 518: parametrize: allow ellipsis to avoid repeating argument list?
https://bitbucket.org/hpk42/pytest/issue/518/parametrize-allow-ellipsis-to-avoid
Matthias Geier:
I'm new to py.test, so please forgive me if this is nonsense ...
I really like py.test and especially the parametrize-feature but I found it gets a bit tedious if I have many arguments in my test function.
The problem is that I basically have to repeat the whole list of arguments: I have to write them once in the string-argument to the `parametrize` decorator and then again in the argument list of the test function itself.
Here's an example:
```
#!python
import pytest
testparams = [
(1, 2, 3, 4, 5, 6, 7, 8),
(8, 7, 6, 5, 4, 3, 2, 1),
]
@pytest.mark.parametrize("a, b, c, d, e, f, g, h", testparams)
def test_many_args(a, b, c, d, e, f, g, h):
assert False
```
I thought it would be nice if the decorator could automatically deduce the list of arguments from the test function, like this:
```
#!python
@pytest.mark.parametrize(..., testparams)
def test_many_args(a, b, c, d, e, f, g, h):
assert False
```
In Python 2.x, you'd have to use `Ellipsis` instead of `...`, of course.
I tried to implement this and came up with a little patch:
```
#!diff
diff -r 95655acb1f90 _pytest/python.py
--- a/_pytest/python.py Mon May 19 20:32:09 2014 +0200
+++ b/_pytest/python.py Sat May 24 16:14:38 2014 +0200
@@ -796,7 +796,9 @@
unwrapped_argvalues.append(argval)
argvalues = unwrapped_argvalues
- if not isinstance(argnames, (tuple, list)):
+ if argnames is Ellipsis:
+ argnames = inspect.getargspec(self.function)[0][:len(argval)]
+ elif not isinstance(argnames, (tuple, list)):
argnames = [x.strip() for x in argnames.split(",") if x.strip()]
if len(argnames) == 1:
argvalues = [(val,) for val in argvalues]
```
I'm sorry that I didn't make a pull request, but I have no clue about Mercurial.
But I guess the change is small enough.
Fixture arguments to the test function do still work, but now they have to be in the end of the argument list, e.g.:
```
#!python
@pytest.fixture
def myfixture():
pass
@pytest.mark.parametrize(..., testparams)
def test_many_args_and_fixture(a, b, c, d, e, f, g, h, myfixture):
assert False
```
What do you think of this idea?
Has something like this been discussed already?
More information about the pytest-commit
mailing list