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

Error using parametrize with default arguments #3221

Closed
ndevenish opened this Issue Feb 14, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@ndevenish

ndevenish commented Feb 14, 2018

With pytest 3.4.0:

import pytest

@pytest.mark.parametrize("something", [True, False])
def test_something(something=False):
  pass

You get the error:

../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:200: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:378: in _genfunctions
    self.ihook.pytest_generate_tests(metafunc=metafunc)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:125: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:805: in parametrize
    self.function, name, arg))
E   ValueError: <function test_something at 0x10e4969b0> uses no argument 'something'

It seems pytest is separating the concept of normal python arguments and 'fixtures' here, only classifying only default-free arguments as potential fixtures - and then only considering 'fixtures' as valid targets for parametrisation - but the error message completely obfuscates this: by any regular python interpretation the function definitely has an argument 'something'.

The documentation on this - https://docs.pytest.org/en/latest/parametrize.html doesn't show this as an example but also doesn't mention that it's a constraint; it's only through reading the pytest source that I could work out what the problem was.

Related: The wording of this was changed to 'argument' from 'fixture' in #1539 which makes sense in the context of that error, but only confuses this one.

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Feb 15, 2018

pytest doesnt consider arguments with default values as targets for fixtures/parameterization

@ndevenish

This comment has been minimized.

ndevenish commented Feb 15, 2018

Yes, that is the root cause. But this isn't a question; my point is that the error is completely confusing and the solution non-obvious unless you read the source code:

pytest doesn't consider arguments with default values

and then complains that there is no argument.

So I'd consider it either a bug with error messages, argument-as-potential-fixture classification (although I assume this was rigorously discussed in the past and is like this for a reason), or a documentation problem.

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Feb 15, 2018

thanks for expressing what you need for better understanding of the issue
i consider it an issue with the error message as well

@feuillemorte

This comment has been minimized.

Member

feuillemorte commented Feb 15, 2018

As I can see pytest ignores arguments with default values:

pytest/_pytest/compat.py

Lines 112 to 115 in e7bcc85

arg_names = tuple(p.name for p in signature(function).parameters.values()
if (p.kind is Parameter.POSITIONAL_OR_KEYWORD or
p.kind is Parameter.KEYWORD_ONLY) and
p.default is Parameter.empty)

Do we need to raise exception here? In this case all arguments with default value will catch this error

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Feb 15, 2018

that function in particular is working exactly as documented, so just raising an error there is not a good way to sort it out

nicoddemus added a commit to nicoddemus/pytest that referenced this issue Apr 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment