[TIP] pytest fixture autouse question

holger krekel holger at merlinux.eu
Fri Jan 4 01:56:42 PST 2013

Hey Chris,

On Thu, Jan 03, 2013 at 14:20 -0500, Pella,Chris wrote:
> I may have a misunderstanding of when session-level autouse fixtures are called. I am trying to create a session level fixture that runs before once per test session before any class setup is called.

It's a known issue of mixing xUnit and pytest style fixtures. See here
for a discussion and the planned resolution:


For now, you can work around the issue like this::

    class TestX:
        @pytest.fixture(scope="class", autouse=True)
        def setup_on_class(self):

This will execute after th "session" scoped installer that you describe


> I have conftest.py file in the directory where the tests reside and in there I have some code that installs some client software on the test node.  We only want to install the client once per session for all the test classes that get executed.
> @pytest.fixture(scope="session",autouse=True)
> def installer_fixture(request):
>     """
>     Installs client at the session level
>     """
>     print "installing"
>     client = InstallClient()
>     client.install()
>     time.sleep(60)
>     def uninstall():
>         """
>         Uninstall client
>         """
>         client.uninstall()
>     request.addfinalizer(uninstall)
> I assumed that this would execute prior to any test setup for a particular test class since it is a session level fixture, but I find that setup_class() executes before the clients get installed.
> e.g. with some toy test code I find  that the class setup gets executed before the fixture.  This is not the behaviour I wanted or expected.
> import pytest
> class TestMe:
>     def setup_class(cls):
>         pytest.set_trace()
>         print "setup"
>     def test_one(cls):
>         print "test one"
>     def teardown_class(cls):
>         print "teardown"
> Thanks,
> chris
> The information contained in this electronic mail transmission 
> may be privileged and confidential, and therefore, protected 
> from disclosure. If you have received this communication in 
> error, please notify us immediately by replying to this 
> message and deleting it from your computer without copying 
> or disclosing it.

> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

More information about the testing-in-python mailing list