繁体   English   中英

Pytest tmpdir_factory 抛出错误“预期二进制或 unicode 字符串,已获取本地”

[英]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.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM