如何创建自己的 pytest fixture?
How do I create my own pytest fixture?
我想创建我自己的 pytest fixture,我可以在其中插入我想要它在设置和拆卸阶段执行的操作。
我正在寻找这样的东西(在这个例子中,我创建了一个测试所需的文件):
@pytest.fixture
def file(path, content):
def setup():
# check that file does NOT exist
if os.path.isfile(path):
raise Exception('file already exists')
# put contents in the file
with open(path, 'w') as file:
file.write(content)
def teardown():
os.remove(path)
我希望能够像这样使用它:
def test_my_function(file):
file('/Users/Me/myapplication/info.txt', 'ham, eggs, orange juice')
assert my_function('info') == ['ham', 'eggs', 'orange juice']
我知道 pytest 中已经有一个具有类似功能的 tempdir
夹具。不幸的是,该固定装置仅在 /tmp
目录中的某处创建文件,而我的应用程序中需要文件。
谢谢!
更新:
我越来越接近了。以下几乎可以工作,但它没有像我预期的那样将全局 PATH 变量设置为夹具。我想知道我是否可以为我的灯具创建一个 class 而不是一个函数。
@pytest.fixture
def file(request):
PATH = None
def setup(path, content):
PATH = path
# check that file does NOT exist
if os.path.isfile(PATH):
raise Exception('file already exists')
# put contents in the file
with open(PATH, 'w+') as file:
file.write(content)
def teardown():
os.remove(PATH)
request.addfinalizer(teardown)
return setup
这有点疯狂,但这里有一个解决方案:
@pytest.fixture
def file(request):
class File:
def __call__(self, path, content):
self.path = path
# check that file does NOT exist
if os.path.isfile(self.path):
raise Exception('file already exists')
# put contents in the file
with open(self.path, 'w+') as file:
file.write(content)
def teardown(self):
os.remove(self.path)
obj = File()
request.addfinalizer(obj.teardown)
return obj
我想创建我自己的 pytest fixture,我可以在其中插入我想要它在设置和拆卸阶段执行的操作。
我正在寻找这样的东西(在这个例子中,我创建了一个测试所需的文件):
@pytest.fixture
def file(path, content):
def setup():
# check that file does NOT exist
if os.path.isfile(path):
raise Exception('file already exists')
# put contents in the file
with open(path, 'w') as file:
file.write(content)
def teardown():
os.remove(path)
我希望能够像这样使用它:
def test_my_function(file):
file('/Users/Me/myapplication/info.txt', 'ham, eggs, orange juice')
assert my_function('info') == ['ham', 'eggs', 'orange juice']
我知道 pytest 中已经有一个具有类似功能的 tempdir
夹具。不幸的是,该固定装置仅在 /tmp
目录中的某处创建文件,而我的应用程序中需要文件。
谢谢!
更新: 我越来越接近了。以下几乎可以工作,但它没有像我预期的那样将全局 PATH 变量设置为夹具。我想知道我是否可以为我的灯具创建一个 class 而不是一个函数。
@pytest.fixture
def file(request):
PATH = None
def setup(path, content):
PATH = path
# check that file does NOT exist
if os.path.isfile(PATH):
raise Exception('file already exists')
# put contents in the file
with open(PATH, 'w+') as file:
file.write(content)
def teardown():
os.remove(PATH)
request.addfinalizer(teardown)
return setup
这有点疯狂,但这里有一个解决方案:
@pytest.fixture
def file(request):
class File:
def __call__(self, path, content):
self.path = path
# check that file does NOT exist
if os.path.isfile(self.path):
raise Exception('file already exists')
# put contents in the file
with open(self.path, 'w+') as file:
file.write(content)
def teardown(self):
os.remove(self.path)
obj = File()
request.addfinalizer(obj.teardown)
return obj