我正在使用pytest来测试如何将数据分割成火车、val、测试集来解决机器学习问题。我使用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"""有人能帮忙吗?谢谢
发布于 2021-01-25 20:38:13
tmpdir_factory夹具方法返回一个py.path.local对象,该对象封装路径(有点模拟到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通常是使用此夹具的方法。
https://stackoverflow.com/questions/65891049
复制相似问题