我使用 py.test 进行测试。
在 setup_class() 中,我需要为我的类构造函数使用 tmpdir:
class TestMyClass:
def setup_class(self):
self.t = MyClass(path=tmpdir)
def test_test(self):
assert True
我有一个错误:
NameError: name 'tmpdir' is not defined
我不能使用 setup_class(self, tmpdir)
。
如果我使用这段代码:
def test_needsfiles(tmpdir):
print(tmpdir)
assert 0
它可以工作,但我的类构造函数中需要 tmpdir。
如何做到这一点?
谢谢!
UPD
我尝试这样做:
@pytest.yield_fixture()
def constructor(tmpdir):
_t = MyClass(path=str(tmpdir))
yield _t
class TestMyClass:
def test_test(self, constructor):
pass
但是我不能在 fixture 中使用作用域:
ScopeMismatch:您尝试使用“模块”范围的请求对象访问“功能”范围的 fixture “tmpdir”,涉及工厂
最佳答案
我这样做:
class TestMyClass:
@pytest.fixture(autouse=True)
def setup(self, tmpdir):
self.tmpdir = tmpdir.strpath
关于python - py.test setup_class 中的 tmpdir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36372034/