From 277b6d3974d4615592c3f67af299d9f90b854ff7 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 2 Aug 2016 19:43:39 -0300 Subject: [PATCH] Sort fixture names when a fixture lookup error occurs --- CHANGELOG.rst | 4 ++++ _pytest/fixtures.py | 2 +- testing/python/fixture.py | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 23185f440..ff6d47440 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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`_). 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 automatically generated id for that argument will be used. Thanks `@palaviv`_ for the complete PR (`#1468`_). diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 427d2e5d9..383e41bad 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -668,7 +668,7 @@ class FixtureLookupError(LookupError): if faclist and name not in available: available.append(name) 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." return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname) diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 0b6829031..d1f507a7f 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -404,6 +404,21 @@ class TestFillFixtures: assert result.ret == 0 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(""" def test_lookup_error(unknown): pass @@ -413,10 +428,12 @@ class TestFillFixtures: "*ERROR*test_lookup_error*", "*def test_lookup_error(unknown):*", "*fixture*unknown*not found*", - "*available fixtures*", + # check if fixtures appear sorted + "*available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*", "*1 error*", ]) 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() def test_fixture_excinfo_leak(self, testdir):