Skip to content

Improved message when not using parametrized variable #1757

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Javier Domingo Cansino
John Towler
Joshua Bronson
Jurko Gospodnetić
Justyna Janczyszyn
Katarzyna Jachim
Kale Kundert
Kevin Cox
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ time or change existing behaviors in order to make them less surprising/more use
* Allow passing a custom debugger class (e.g. ``--pdbcls=IPython.core.debugger:Pdb``).
Thanks to `@anntzer`_ for the PR.


*

*
Expand Down Expand Up @@ -237,6 +236,9 @@ time or change existing behaviors in order to make them less surprising/more use

* ``optparse`` backward compatibility supports float/complex types (`#457`_).

* Better message in case of not using parametrized variable (see `#1539`_).
Thanks to `@tramwaj29`_ for the PR.

*

*
Expand Down Expand Up @@ -318,6 +320,7 @@ time or change existing behaviors in order to make them less surprising/more use
.. _#1664: https://github.com/pytest-dev/pytest/pull/1664
.. _#1684: https://github.com/pytest-dev/pytest/pull/1684
.. _#1723: https://github.com/pytest-dev/pytest/pull/1723
.. _#1539: https://github.com/pytest-dev/pytest/issues/1539
.. _#1749: https://github.com/pytest-dev/pytest/issues/1749

.. _@DRMacIver: https://github.com/DRMacIver
Expand Down Expand Up @@ -351,6 +354,7 @@ time or change existing behaviors in order to make them less surprising/more use
.. _@tareqalayan: https://github.com/tareqalayan
.. _@taschini: https://github.com/taschini
.. _@txomon: https://github.com/txomon
.. _@tramwaj29: https://github.com/tramwaj29

2.9.2
=====
Expand Down
10 changes: 8 additions & 2 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,13 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
valtypes = {}
for arg in argnames:
if arg not in self.fixturenames:
raise ValueError("%r uses no fixture %r" %(self.function, arg))
if isinstance(indirect, (tuple, list)):
name = 'fixture' if arg in indirect else 'argument'
else:
name = 'fixture' if indirect else 'argument'
raise ValueError(
"%r uses no %s %r" % (
self.function, name, arg))

if indirect is True:
valtypes = dict.fromkeys(argnames, "params")
Expand All @@ -811,7 +817,7 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
valtypes = dict.fromkeys(argnames, "funcargs")
for arg in indirect:
if arg not in argnames:
raise ValueError("indirect given to %r: fixture %r doesn't exist" %(
raise ValueError("indirect given to %r: fixture %r doesn't exist" % (
self.function, arg))
valtypes[arg] = "params"
idfn = None
Expand Down
38 changes: 36 additions & 2 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_simple(x):
""")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines([
"*uses no fixture 'y'*",
"*uses no argument 'y'*",
])

@pytest.mark.issue714
Expand All @@ -417,6 +417,23 @@ def test_simple(x):
"*uses no fixture 'y'*",
])

@pytest.mark.issue714
def test_parametrize_indirect_uses_no_fixture_error_indirect_string(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(scope='function')
def x(request):
return request.param * 3

@pytest.mark.parametrize('x, y', [('a', 'b')], indirect='y')
def test_simple(x):
assert len(x) == 3
""")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines([
"*uses no fixture 'y'*",
])

@pytest.mark.issue714
def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir):
testdir.makepyfile("""
Expand All @@ -425,7 +442,7 @@ def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir)
def x(request):
return request.param * 3

@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x'])
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['y'])
def test_simple(x):
assert len(x) == 3
""")
Expand All @@ -434,6 +451,23 @@ def test_simple(x):
"*uses no fixture 'y'*",
])

@pytest.mark.issue714
def test_parametrize_argument_not_in_indirect_list(self, testdir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a test when indirect is just a string.

@pytest.mark.issue714
def test_parametrize_argument_not_in_indirect_list(self, testdir):
    for decl in ("['x']", "'x'"):
        testdir.makepyfile("""
            import pytest
            @pytest.fixture(scope='function')
            def x(request):
                return request.param * 3
            @pytest.mark.parametrize('x, y', [('a', 'b')], indirect={decl}
            def test_simple(x):
                assert len(x) == 3
        """.format(decl=decl))
        result = testdir.runpytest("--collect-only")
        result.stdout.fnmatch_lines([
            "*uses no argument 'y'*",
        ])

It is a shame that we can't use parametrize here (we try to avoid using parametrize in this test module);

testdir.makepyfile("""
import pytest
@pytest.fixture(scope='function')
def x(request):
return request.param * 3

@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x'])
def test_simple(x):
assert len(x) == 3
""")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines([
"*uses no argument 'y'*",
])

def test_addcalls_and_parametrize_indirect(self):
def func(x, y): pass
metafunc = self.Metafunc(func)
Expand Down