Sort fixture names when a fixture lookup error occurs

This commit is contained in:
Bruno Oliveira 2016-08-02 19:43:39 -03:00
parent ea6191a0cd
commit 277b6d3974
3 changed files with 23 additions and 2 deletions

View File

@ -178,6 +178,10 @@ time or change existing behaviors in order to make them less surprising/more use
* Explicitly passed parametrize ids do not get escaped to ascii (`#1351`_). * Explicitly passed parametrize ids do not get escaped to ascii (`#1351`_).
Thanks `@ceridwen`_ for the PR. Thanks `@ceridwen`_ for the PR.
* Fixtures are now sorted in the error message displayed when an unknown
fixture is declared in a test function.
Thanks `@nicoddemus`_ for the PR.
* Parametrize ids can accept ``None`` as specific test id, in which case the * Parametrize ids can accept ``None`` as specific test id, in which case the
automatically generated id for that argument will be used. automatically generated id for that argument will be used.
Thanks `@palaviv`_ for the complete PR (`#1468`_). Thanks `@palaviv`_ for the complete PR (`#1468`_).

View File

@ -668,7 +668,7 @@ class FixtureLookupError(LookupError):
if faclist and name not in available: if faclist and name not in available:
available.append(name) available.append(name)
msg = "fixture %r not found" % (self.argname,) msg = "fixture %r not found" % (self.argname,)
msg += "\n available fixtures: %s" %(", ".join(available),) msg += "\n available fixtures: %s" %(", ".join(sorted(available)),)
msg += "\n use 'pytest --fixtures [testpath]' for help on them." msg += "\n use 'pytest --fixtures [testpath]' for help on them."
return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname) return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname)

View File

@ -404,6 +404,21 @@ class TestFillFixtures:
assert result.ret == 0 assert result.ret == 0
def test_funcarg_lookup_error(self, testdir): def test_funcarg_lookup_error(self, testdir):
testdir.makeconftest("""
import pytest
@pytest.fixture
def a_fixture(): pass
@pytest.fixture
def b_fixture(): pass
@pytest.fixture
def c_fixture(): pass
@pytest.fixture
def d_fixture(): pass
""")
testdir.makepyfile(""" testdir.makepyfile("""
def test_lookup_error(unknown): def test_lookup_error(unknown):
pass pass
@ -413,10 +428,12 @@ class TestFillFixtures:
"*ERROR*test_lookup_error*", "*ERROR*test_lookup_error*",
"*def test_lookup_error(unknown):*", "*def test_lookup_error(unknown):*",
"*fixture*unknown*not found*", "*fixture*unknown*not found*",
"*available fixtures*", # check if fixtures appear sorted
"*available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*",
"*1 error*", "*1 error*",
]) ])
assert "INTERNAL" not in result.stdout.str() assert "INTERNAL" not in result.stdout.str()
# invocation-scoped fixture should appear with their friendly name only
assert 'monkeypatch:session' not in result.stdout.str() assert 'monkeypatch:session' not in result.stdout.str()
def test_fixture_excinfo_leak(self, testdir): def test_fixture_excinfo_leak(self, testdir):