gpt4 book ai didi

python - 将(产量)装置作为测试参数传递(使用临时目录)

转载 作者:行者123 更新时间:2023-11-28 21:40:50 26 4
gpt4 key购买 nike

问题

是否可以将 yielding pytest fixtures(用于设置和拆卸)作为参数传递给测试函数?

上下文

我正在测试一个对象,该对象从单个目录中的文件读取数据/向文件写入数据。该目录的路径被保存为对象的属性。

我遇到以下问题:

  1. 在我的测试中使用一个临时目录;和
  2. 确保在每次测试后删除该目录。

例子

考虑以下 (test_yieldfixtures.py):

import pytest, tempfile, os, shutil
from contextlib import contextmanager

@contextmanager
def data():
datadir = tempfile.mkdtemp() # setup
yield datadir
shutil.rmtree(datadir) # teardown

class Thing:
def __init__(self, datadir, errorfile):
self.datadir = datadir
self.errorfile = errorfile


@pytest.fixture
def thing1():
with data() as datadir:
errorfile = os.path.join(datadir, 'testlog1.log')
yield Thing(datadir=datadir, errorfile=errorfile)

@pytest.fixture
def thing2():
with data() as datadir:
errorfile = os.path.join(datadir, 'testlog2.log')
yield Thing(datadir=datadir, errorfile=errorfile)

@pytest.mark.parametrize('thing', [thing1, thing2])
def test_attr(thing):
print(thing.datadir)
assert os.path.exists(thing.datadir)

运行 pytest test_yieldfixtures.py 输出如下:

================================== FAILURES ===================================
______________________________ test_attr[thing0] ______________________________

thing = <generator object thing1 at 0x0000017B50C61BF8>

@pytest.mark.parametrize('thing', [thing1, thing2])
def test_attr(thing):
> print(thing.datadir)
E AttributeError: 'function' object has no attribute 'props'

test_mod.py:39: AttributeError

好的。所以 fixture 函数没有我的类的属性。很公平。

尝试 1

函数没有属性,所以我尝试调用该函数来实际获取对象。然而,那只是

@pytest.mark.parametrize('thing', [thing1(), thing2()])
def test_attr(thing):
print(thing.props['datadir'])
assert os.path.exists(thing.get('datadir'))

结果:

================================== FAILURES ===================================
______________________________ test_attr[thing0] ______________________________

thing = <generator object thing1 at 0x0000017B50C61BF8>

@pytest.mark.parametrize('thing', [thing1(), thing2()])
def test_attr(thing):
> print(thing.datadir)
E AttributeError: 'generator' object has no attribute 'props'

test_mod.py:39: AttributeError

尝试 2

我还尝试在 thing1/2 固定装置中使用 return 而不是 yield ,但这使我退出了 data 上下文管理器并删除目录:

================================== FAILURES ===================================
______________________________ test_attr[thing0] ______________________________

thing = <test_mod.Thing object at 0x000001C528F05358>

@pytest.mark.parametrize('thing', [thing1(), thing2()])
def test_attr(thing):
print(thing.datadir)
> assert os.path.exists(thing.datadir)

结束语

重述问题:是否有办法将这些固定装置作为参数传递并保持临时目录的清理?

最佳答案

尝试将您的data 函数/生成器制作成固定装置。然后使用 request.getfixturevalue()动态运行命名的 fixture 。

import pytest, tempfile, os, shutil
from contextlib import contextmanager

@pytest.fixture # This works with pytest>3.0, on pytest<3.0 use yield_fixture
def datadir():
datadir = tempfile.mkdtemp() # setup
yield datadir
shutil.rmtree(datadir) # teardown

class Thing:
def __init__(self, datadir, errorfile):
self.datadir = datadir
self.errorfile = errorfile


@pytest.fixture
def thing1(datadir):
errorfile = os.path.join(datadir, 'testlog1.log')
yield Thing(datadir=datadir, errorfile=errorfile)

@pytest.fixture
def thing2(datadir):
errorfile = os.path.join(datadir, 'testlog2.log')
yield Thing(datadir=datadir, errorfile=errorfile)

@pytest.mark.parametrize('thing_fixture_name', ['thing1', 'thing2'])
def test_attr(request, thing):
thing = request.getfixturevalue(thing) # This works with pytest>3.0, on pytest<3.0 use getfuncargvalue
print(thing.datadir)
assert os.path.exists(thing.datadir)

更进一步,您可以像这样参数化thing fixtures:

class Thing:
def __init__(self, datadir, errorfile):
self.datadir = datadir
self.errorfile = errorfile

@pytest.fixture(params=['test1.log', 'test2.log'])
def thing(request):
with tempfile.TemporaryDirectory() as datadir:
errorfile = os.path.join(datadir, request.param)
yield Thing(datadir=datadir, errorfile=errorfile)

def test_thing_datadir(thing):
assert os.path.exists(thing.datadir)

关于python - 将(产量)装置作为测试参数传递(使用临时目录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45225950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com