Skip to content

Commit d5be6cb

Browse files
authored
Merge pull request #1788 from nicoddemus/available-fixtures-sorted
Sort fixture names when a fixture lookup error occurs
2 parents ea6191a + 277b6d3 commit d5be6cb

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ time or change existing behaviors in order to make them less surprising/more use
178178
* Explicitly passed parametrize ids do not get escaped to ascii (`#1351`_).
179179
Thanks `@ceridwen`_ for the PR.
180180

181+
* Fixtures are now sorted in the error message displayed when an unknown
182+
fixture is declared in a test function.
183+
Thanks `@nicoddemus`_ for the PR.
184+
181185
* Parametrize ids can accept ``None`` as specific test id, in which case the
182186
automatically generated id for that argument will be used.
183187
Thanks `@palaviv`_ for the complete PR (`#1468`_).

_pytest/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def formatrepr(self):
668668
if faclist and name not in available:
669669
available.append(name)
670670
msg = "fixture %r not found" % (self.argname,)
671-
msg += "\n available fixtures: %s" %(", ".join(available),)
671+
msg += "\n available fixtures: %s" %(", ".join(sorted(available)),)
672672
msg += "\n use 'pytest --fixtures [testpath]' for help on them."
673673

674674
return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname)

testing/python/fixture.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,21 @@ def test_foo(request):
404404
assert result.ret == 0
405405

406406
def test_funcarg_lookup_error(self, testdir):
407+
testdir.makeconftest("""
408+
import pytest
409+
410+
@pytest.fixture
411+
def a_fixture(): pass
412+
413+
@pytest.fixture
414+
def b_fixture(): pass
415+
416+
@pytest.fixture
417+
def c_fixture(): pass
418+
419+
@pytest.fixture
420+
def d_fixture(): pass
421+
""")
407422
testdir.makepyfile("""
408423
def test_lookup_error(unknown):
409424
pass
@@ -413,10 +428,12 @@ def test_lookup_error(unknown):
413428
"*ERROR*test_lookup_error*",
414429
"*def test_lookup_error(unknown):*",
415430
"*fixture*unknown*not found*",
416-
"*available fixtures*",
431+
# check if fixtures appear sorted
432+
"*available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*",
417433
"*1 error*",
418434
])
419435
assert "INTERNAL" not in result.stdout.str()
436+
# invocation-scoped fixture should appear with their friendly name only
420437
assert 'monkeypatch:session' not in result.stdout.str()
421438

422439
def test_fixture_excinfo_leak(self, testdir):

0 commit comments

Comments
 (0)