[英]Pytest tmpdir_factory threw an error “Expected binary or unicode string, got local”
我正在使用pytest
对将数据拆分为机器学习问题的训练、验证和测试集进行测试。 我使用tmpdir_factory
创建临时文件,但它给我一个错误,例如TypeError: Expected binary or unicode string, got local('/tmp/pytest/pytest-4/test_folder0/train.tfrecord')
。 这是我的代码:
在conftest.py
里面:
DATA_FOLDER = 'test_folder'
@pytest.fixture(scope="session")
def train_dataset(tmpdir_factory):
return tmpdir_factory.mktemp(DATA_FOLDER).join('train.tfrecord')
@pytest.fixture(scope="session")
def val_dataset(tmpdir_factory):
return tmpdir_factory.mktemp(DATA_FOLDER).join('val.tfrecord')
@pytest.fixture(scope="session")
def test_dataset(tmpdir_factory):
return tmpdir_factory.mktemp(DATA_FOLDER).join('test.tfrecord')
在测试文件里面:
def test_split(train_dataset, val_dataset, test_dataset):
# the arguments of split_function refer to the path where the splitting results is written
split_function(train_dataset, val_dataset, test_dataset)
"""continue with assert functions"""
有人可以帮忙吗? 谢谢
tmpdir_factory
夹具方法返回一个py.path.local object,它封装了一个路径(有点pathlib.Path
)。 因此,可以链接这些方法调用来操作路径,就像在您的夹具中使用mktemp().join()
所做的那样。 要从结果中取回str
路径,您必须显式地将py.path.local
转换为str
:
@pytest.fixture(scope="session")
def train_dataset(tmpdir_factory):
return str(tmpdir_factory.mktemp(DATA_FOLDER).join('train.tfrecord'))
由于您测试的函数不了解py.path.local
,因此将tmpdir_factory
创建的路径转换回str
通常是使用此夹具的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.