Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tmpdir in py.test setup_class

Tags:

python

pytest

I use py.test for testing.

In setup_class() I need to use tmpdir for my class constructor:

class TestMyClass:
    def setup_class(self):
        self.t = MyClass(path=tmpdir)

    def test_test(self):
        assert True

And I have an error:

NameError: name 'tmpdir' is not defined

I can't use setup_class(self, tmpdir).

If I use this code:

def test_needsfiles(tmpdir):
    print(tmpdir)
    assert 0

It's work, but I need tmpdir in my class constructor.

How to do this?

Thanks!

UPD

I try to do this:

@pytest.yield_fixture()
def constructor(tmpdir):
    _t = MyClass(path=str(tmpdir))
    yield _t

class TestMyClass:

    def test_test(self, constructor):
        pass

But I can't use scopes in fixture:

ScopeMismatch: You tried to access the 'function' scoped fixture 'tmpdir' with a 'module' scoped request object, involved factories

like image 850
Lev Avatar asked Oct 30 '22 06:10

Lev


1 Answers

I do this:

class TestMyClass:
    @pytest.fixture(autouse=True)
    def setup(self, tmpdir):
        self.tmpdir = tmpdir.strpath
like image 187
santon Avatar answered Nov 09 '22 21:11

santon