Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixture execution order #805

Closed
kracekumar opened this issue Jun 26, 2015 · 4 comments
Closed

Fixture execution order #805

kracekumar opened this issue Jun 26, 2015 · 4 comments

Comments

@kracekumar
Copy link

import pytest


class Meh(object):
    @pytest.fixture(autouse=True)
    def a(self, request):
        print("meh")


class Foo(object):
    @pytest.fixture(autouse=True)
    def b(self):
        print("foo")


class Bar(object):
    @pytest.fixture(autouse=True)
    def c(self):
        print("bar")


class TestBaz(Meh, Bar, Foo):
    @pytest.fixture(autouse=True)
    def d(self):
        print("baz")

    def test_1(self):
        assert 1 == 1


class Meh1(object):
    @pytest.fixture(autouse=True)
    def foo(self, request):
        print("meh")


class Foo1(object):
    @pytest.fixture(autouse=True)
    def one(self):
        print("foo")


class Bar1(object):
    @pytest.fixture(autouse=True)
    def two(self):
        print("bar")


class TestBaz1(Meh1, Bar1, Foo1):
    @pytest.fixture(autouse=True)
    def three(self):
        print("baz")

    def test_1(self):
        assert 1 == 1

Output

(score)➜  score git:(master) ✗ py.test -s -v tests/integration_tests/test_test.py --pdb
============================================================================================================================= test session starts ==============================================================================================================================
platform darwin -- Python 2.7.6 -- py-1.4.27 -- pytest-2.7.0 -- /Users/krace/.virtualenvs/score/bin/python
rootdir: /Users/krace/code/score, inifile:
collected 2 items

tests/integration_tests/test_test.py::TestBaz::test_1 meh
foo
bar
baz
PASSED
tests/integration_tests/test_test.py::TestBaz1::test_1 meh
foo
baz
bar
PASSED

=========================================================================================================================== 2 passed in 0.03 seconds ===========================================================================================================================

I am using multiple classes with mixin approach to write test cases. Fixtures are executed alphabetically, not in the order.

I am expecting the fixtures to be run in Meh1 -> Foo1 -> Bar1 -> TestBaz1. Adding scope=class doesn't help. What am I doing wrong ?

@nicoddemus
Copy link
Member

pytest currently doesn't try to take in account subclasses when executing fixtures.

One way to "enforce" one fixture to execute before the other is to make each fixture depend on a fixture of its subclass, for example make b fixture receive a a argument, or use pytest.mark.usefixtures.

One of the design goals for fixtures is modularity, so that fixtures can be used in different contexts without test functions knowing about them. An advantage of this is that fixtures can easily be defined at a module first and then later, when proven useful to be used in other tests/modules, moved to a conftest easily without changing tests themselves.

Hope that helps! 😄

@kracekumar
Copy link
Author

Thanks. If it makes sense can we explicit add subclassing fixtures won't work in FAQ ?

@nicoddemus
Copy link
Member

Sure, good idea. Could you open a PR? 😄

@kracekumar
Copy link
Author

Yes, I will do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants