Skip to content

Error using parametrize with default arguments #3221

Closed
@ndevenish

Description

@ndevenish

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: help wanteddevelopers would like help from experts on this topictopic: parametrizerelated to @pytest.mark.parametrizetype: bugproblem that needs to be addressedtype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions