簡體   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-2024 STACKOOM.COM