当两个测试读取同一文件时,pytest-xdis 失败

问题描述 投票:0回答:1

我有两个涉及读取同一文件的测试用例(它们调用一个将 JSON 文件加载为类的函数)。

功能是:

import os
import json

def load_json(relative_file_path: str) -> Dict[str, Any]:
    current_file_path = os.path.dirname(__file__)
    file_to_open_path = os.path.join(current_file_path, relative_file_path)
    with open(file_to_open_path, 'r') as opened_file:
        return json.load(opened_file)

当我使用 pytest-xdist 和

-n=logical
运行时,它们会失败,但是当我连续运行时 (
-n=0
),它们会通过。

失败消息是:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我在负载周围添加了一个 try catch,我得到:

Cannot load file to json, file is

我认为这意味着一个测试的读取器与第二个测试的读取器发生冲突,导致第二次读取时它看起来位于文件末尾。

我在打开文件时做了什么以允许并行读取吗?

json file-io pytest-xdist
1个回答
0
投票

经过进一步挖掘,似乎另一个测试正在写入文件,所以我认为我遇到了冲突

© www.soinside.com 2019 - 2024. All rights reserved.