[TIP] pytest fixture autouse question

Pella,Chris Chris.Pella at safenet-inc.com
Wed Jan 9 14:29:23 PST 2013



> -----Original Message-----
> From: holger krekel [mailto:holger at merlinux.eu]
> Sent: January-04-13 4:57 AM
> To: Pella,Chris
> Cc: testing-in-python at lists.idyll.org
> Subject: Re: [TIP] pytest fixture autouse question
> 
> 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:
> 
>     http://mail.python.org/pipermail/pytest-dev/2012-December/002172.html
> 
> For now, you can work around the issue like this::
> 
>     class TestX:
>         @pytest.fixture(scope="class", autouse=True)
>         def setup_on_class(self):
>             ...
> 
I now see a problem with this workaround ... e.g. If I run the following code it will throw an exception because cls.skip_me is no longer accessible. 

class TestThis():
    @pytest.fixture(scope="class", autouse=True)
    def setup_class(cls):
        #cls.skip_me = True 
        cls.skip_me = False

    def test_one(cls):
        if cls.skip_me == True:
            pytest.skip("can't test this")
        assert 1

    def test_two(cls):
        if cls.skip_me == True:
            pytest.skip("can't test this")
        assert 1

    def teardown_class(cls):
        pass


> This will execute after th "session" scoped installer that you describe below.
> 
> cheers,
> holger
> 
> > 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

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.





More information about the testing-in-python mailing list