[pytest-dev] Access fixtures from unittest.TestCase

holger krekel holger at merlinux.eu
Mon Apr 7 12:24:27 CEST 2014


Hi Wolfgang,

On Fri, Apr 04, 2014 at 11:21 +0200, Wolfgang Schnerring wrote:
> Hello,
> 
> is there a way to access a fixture object from inside a unitest.TestCase test
> function? As far as I understand, the way to combine fixtures and TestCase is
> like this:
> 
>   import pytest, unittest
> 
>   @pytest.mark.usefixtures('tmpdir')
>   class MyTest(unittest.TestCase):
> 
>        def test_something(self):
>            # XXX how can I get to ``tmpdir`` here?
> 
> But there seems to be no way to get to the fixture object, which is rather
> constraining. Or am I missing something?

Right, any "usefixture" needs to either have a global side effect
or stick some object to "self".  One easier way to get a tempdir
is this:

   class MyTest(unittest.TestCase):
   
        @pytest.fixture(autouse=True)
        def set_tmpdir(self, tmpdir):
            self.tmpdir = tmpdir

        def test_something(self):
            # access self.tmpdir

HTH,
holger


> Thanks for your help,
> Wolfgang
> 
> _______________________________________________
> Pytest-dev mailing list
> Pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
> 


More information about the Pytest-dev mailing list