Skip to content

Commit a24146d

Browse files
authored
Merge pull request #1757 from tramwaj29/improved-message-when-not-using-parametrized-variable
Improved message when not using parametrized variable
2 parents 7862517 + 4aede6f commit a24146d

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Javier Domingo Cansino
6565
John Towler
6666
Joshua Bronson
6767
Jurko Gospodnetić
68+
Justyna Janczyszyn
6869
Katarzyna Jachim
6970
Kale Kundert
7071
Kevin Cox

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ time or change existing behaviors in order to make them less surprising/more use
149149
* Allow passing a custom debugger class (e.g. ``--pdbcls=IPython.core.debugger:Pdb``).
150150
Thanks to `@anntzer`_ for the PR.
151151

152-
153152
*
154153

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

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

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

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

323326
.. _@DRMacIver: https://github.com/DRMacIver
@@ -351,6 +354,7 @@ time or change existing behaviors in order to make them less surprising/more use
351354
.. _@tareqalayan: https://github.com/tareqalayan
352355
.. _@taschini: https://github.com/taschini
353356
.. _@txomon: https://github.com/txomon
357+
.. _@tramwaj29: https://github.com/tramwaj29
354358

355359
2.9.2
356360
=====

_pytest/python.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,13 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
801801
valtypes = {}
802802
for arg in argnames:
803803
if arg not in self.fixturenames:
804-
raise ValueError("%r uses no fixture %r" %(self.function, arg))
804+
if isinstance(indirect, (tuple, list)):
805+
name = 'fixture' if arg in indirect else 'argument'
806+
else:
807+
name = 'fixture' if indirect else 'argument'
808+
raise ValueError(
809+
"%r uses no %s %r" % (
810+
self.function, name, arg))
805811

806812
if indirect is True:
807813
valtypes = dict.fromkeys(argnames, "params")
@@ -811,7 +817,7 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
811817
valtypes = dict.fromkeys(argnames, "funcargs")
812818
for arg in indirect:
813819
if arg not in argnames:
814-
raise ValueError("indirect given to %r: fixture %r doesn't exist" %(
820+
raise ValueError("indirect given to %r: fixture %r doesn't exist" % (
815821
self.function, arg))
816822
valtypes[arg] = "params"
817823
idfn = None

testing/python/metafunc.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_simple(x):
394394
""")
395395
result = testdir.runpytest("--collect-only")
396396
result.stdout.fnmatch_lines([
397-
"*uses no fixture 'y'*",
397+
"*uses no argument 'y'*",
398398
])
399399

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

420+
@pytest.mark.issue714
421+
def test_parametrize_indirect_uses_no_fixture_error_indirect_string(self, testdir):
422+
testdir.makepyfile("""
423+
import pytest
424+
@pytest.fixture(scope='function')
425+
def x(request):
426+
return request.param * 3
427+
428+
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect='y')
429+
def test_simple(x):
430+
assert len(x) == 3
431+
""")
432+
result = testdir.runpytest("--collect-only")
433+
result.stdout.fnmatch_lines([
434+
"*uses no fixture 'y'*",
435+
])
436+
420437
@pytest.mark.issue714
421438
def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir):
422439
testdir.makepyfile("""
@@ -425,7 +442,7 @@ def test_parametrize_indirect_uses_no_fixture_error_indirect_list(self, testdir)
425442
def x(request):
426443
return request.param * 3
427444
428-
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x'])
445+
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['y'])
429446
def test_simple(x):
430447
assert len(x) == 3
431448
""")
@@ -434,6 +451,23 @@ def test_simple(x):
434451
"*uses no fixture 'y'*",
435452
])
436453

454+
@pytest.mark.issue714
455+
def test_parametrize_argument_not_in_indirect_list(self, testdir):
456+
testdir.makepyfile("""
457+
import pytest
458+
@pytest.fixture(scope='function')
459+
def x(request):
460+
return request.param * 3
461+
462+
@pytest.mark.parametrize('x, y', [('a', 'b')], indirect=['x'])
463+
def test_simple(x):
464+
assert len(x) == 3
465+
""")
466+
result = testdir.runpytest("--collect-only")
467+
result.stdout.fnmatch_lines([
468+
"*uses no argument 'y'*",
469+
])
470+
437471
def test_addcalls_and_parametrize_indirect(self):
438472
def func(x, y): pass
439473
metafunc = self.Metafunc(func)

0 commit comments

Comments
 (0)