diff --git a/AUTHORS b/AUTHORS index cd9678b3f97..b6b6b46faa2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -91,6 +91,7 @@ Kale Kundert Katarzyna Jachim Kevin Cox Kodi B. Arfer +Lawrence Mitchell Lee Kamentsky Lev Maximov Llandy Riveron Del Risco diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 5505fb4e19e..d475dfd1bda 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -19,6 +19,11 @@ from _pytest.runner import fail from _pytest.compat import FuncargnamesCompatAttr +if sys.version_info[:2] == (2, 6): + from ordereddict import OrderedDict +else: + from collections import OrderedDict + def pytest_sessionstart(session): import _pytest.python @@ -136,10 +141,10 @@ def get_parametrized_fixture_keys(item, scopenum): except AttributeError: pass else: - # cs.indictes.items() is random order of argnames but - # then again different functions (items) can change order of - # arguments so it doesn't matter much probably - for argname, param_index in cs.indices.items(): + # cs.indices.items() is random order of argnames. Need to + # sort this so that different calls to + # get_parametrized_fixture_keys will be deterministic. + for argname, param_index in sorted(cs.indices.items()): if cs._arg2scopenum[argname] != scopenum: continue if scopenum == 0: # session @@ -161,7 +166,7 @@ def reorder_items(items): for scopenum in range(0, scopenum_function): argkeys_cache[scopenum] = d = {} for item in items: - keys = set(get_parametrized_fixture_keys(item, scopenum)) + keys = OrderedDict.fromkeys(get_parametrized_fixture_keys(item, scopenum)) if keys: d[item] = keys return reorder_items_atscope(items, set(), argkeys_cache, 0) @@ -196,9 +201,9 @@ def slice_items(items, ignore, scoped_argkeys_cache): for i, item in enumerate(it): argkeys = scoped_argkeys_cache.get(item) if argkeys is not None: - argkeys = argkeys.difference(ignore) - if argkeys: # found a slicing key - slicing_argkey = argkeys.pop() + newargkeys = OrderedDict.fromkeys(k for k in argkeys if k not in ignore) + if newargkeys: # found a slicing key + slicing_argkey, _ = newargkeys.popitem() items_before = items[:i] items_same = [item] items_other = [] diff --git a/changelog/920.bugfix b/changelog/920.bugfix new file mode 100644 index 00000000000..efe646a6e0e --- /dev/null +++ b/changelog/920.bugfix @@ -0,0 +1 @@ +Fix non-determinism in order of fixture collection. diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 1801c91ffb9..f8aef802fa4 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -2548,7 +2548,6 @@ def test_foo(fix): '*test_foo*beta*']) @pytest.mark.issue920 - @pytest.mark.xfail(reason="Fixture reordering not deterministic with hash randomisation") def test_deterministic_fixture_collection(self, testdir, monkeypatch): testdir.makepyfile(""" import pytest diff --git a/tox.ini b/tox.ini index 3dce17b344b..1307cff143a 100644 --- a/tox.ini +++ b/tox.ini @@ -34,6 +34,7 @@ deps = hypothesis<3.0 nose mock<1.1 + ordereddict [testenv:py27-subprocess] changedir = .