pytest 模拟 pathlib.Path.open

pytest-mock pathlib.Path.open

我需要使用 pytest-mock 模拟 pathlib.Path.open

真正的open_func开一个yaml-file。 return 值是一个常规值 dict。我如何模拟 Path.open 来加载另一个名为 test-config.yamlyaml-file

我的代码无法正常工作,因为 conf 只会变成 str ("test_config.yaml")。应该是 dict.

from pathlib import Path

import yaml


def open_func():
    with Path.open(Path("./config.yaml")) as f:
        return yaml.load(f, Loader=yaml.FullLoader)


def test_open_func(mocker):
    mocker.patch("pathlib.Path.open", mocker.mock_open(read_data="test_config.yaml"))
    conf = open_func()

    assert isinstance(conf, dict)

编辑: 为了更接近我的现实世界问题,我提供了以下代码。我有一个 class TryToMock,它基本上将两个文件作为输入。 load_files 方法只是加载这些文件(实际上是 .yaml 文件)并 return 输出。这些.yaml文件实际上是一些配置文件。

在我的单元测试中,我将通过 pytest 的 parametrize 调用 TryToMock 无数次。因此,我想通过 fixture 加载原始配置文件。然后我可以在 运行 load_files.

之前的各种测试中 monkeypatch 一些条目

为了不再次加载原文件,我需要mockTryToMock中的Path.open函数。我想传递 monkeypatched yaml 文件(即 dict 的形式)。困难在于我必须区分这两个文件。那就是我不能简单地用相同的文件内容模拟 Path.open 函数。

# TryToMock.py

from pathlib import Path
import yaml

# In my current working folder, I have to .yaml files containing the following
# content for illustrative purpose:
#
# file1.yaml = {'name': 'test1', 'file_type': 'yaml'}
# file2.yaml = {'schema': 'test2', 'currencies': ['EUR', 'USD', 'JPY']}


class TryToMock:
    def __init__(self, file_to_mock_1, file_to_mock_2):
        self._file_to_mock_1 = file_to_mock_1
        self._file_to_mock_2 = file_to_mock_2

    def load_files(self):
        with Path.open(self._file_to_mock_1) as f:
            file1 = yaml.load(f, Loader=yaml.FullLoader)

        with Path.open(self._file_to_mock_2) as f:
            file2 = yaml.load(f, Loader=yaml.FullLoader)

        return file1, file2




# test_TryToMock.py

import os
from pathlib import Path

import pytest
import yaml

from tests import TryToMock


def yaml_files_for_test(yaml_content):
    names = {"file1.yaml": file1_content, "file2.yaml": file2_content}
    return os.path.join("./", names[os.path.basename(yaml_content)])


@pytest.fixture(scope="module")
def file1_content():
    with Path.open(Path("./file1.yaml")) as f:
        return yaml.load(f, Loader=yaml.FullLoader)


@pytest.fixture(scope="module")
def file2_content():
    with Path.open(Path("./file2.yaml")) as f:
        return yaml.load(f, Loader=yaml.FullLoader)


def test_try_to_mock(file1_content, file2_content, monkeypatch, mocker):
    file_1 = Path("./file1.yaml")
    file_2 = Path("./file2.yaml")

    m = TryToMock.TryToMock(file_to_mock_1=file_1, file_to_mock_2=file_2)

    # Change some items
    monkeypatch.setitem(file1_content, "file_type", "json")

    # Mocking - How does it work when I would like to use mock_open???
    # How should the lambda function look like?
    mocker.patch(
        "pathlib.Path.open",
        lambda x: mocker.mock_open(read_data=yaml_files_for_test(x)),
    )

    files = m.load_files()
    assert files[0]["file_type"] == "json"

您必须将实际文件内容提供给 mock_openread_data 参数。您可以只在测试中创建数据:

test_yaml = """
foo:
  bar:
    - VAR: "MyVar"
"""

def test_open_func(mocker):
    mocker.patch("pathlib.Path.open", mocker.mock_open(read_data=test_yaml))
    conf = open_func()
    assert conf == {'foo': {'bar': [{'VAR': 'MyVar'}]}}

或者您可以从测试文件中读取数据:

def test_open_func(mocker):
    with open("my_fixture_path/test.yaml") as f:
        contents = f.read()
    mocker.patch("pathlib.Path.open", mocker.mock_open(read_data=contents))
    conf = open_func()
    assert isinstance(conf, dict)

最后一个案例也可以重写以替换测试路径 open 调用中的 path 参数:

def test_open_func(mocker):
    mocker.patch("pathlib.Path.open", lambda path: open("test.yaml"))
    conf = open_func()
    assert isinstance(conf, dict)

或者,如果您有针对不同配置的不同测试文件,例如:

def yaml_path_for_test(yaml_path):
    names = {
        "config.yaml": "test.yaml",
        ...
    }
    return os.path.join(my_fixture_path, names[os.path.basename(yaml_path)])

def test_open_func3(mocker):
    mocker.patch("pathlib.Path.open", lambda path: open(yaml_path_for_test(path)))
    conf = open_func()
    assert isinstance(conf, dict)

这可能是您希望在测试代码中实现的。

更新: 这与问题的第二部分(编辑后)有关。如果您有预加载夹具文件的模块范围的夹具,那么您可以这样做:

def test_open_func(mocker, file1_content, file2_content):
    def yaml_files_for_test(path):
        contents = {"file1.yaml": file1_content,
                    "file2.yaml": file2_content}
        data = contents[os.path.basename(path)]
        mock = mocker.mock_open(read_data=yaml.dump(data))
        return mock.return_value

    mocker.patch("pathlib.Path.open", yaml_files_for_test)
    conf = open_func()
    assert isinstance(conf, dict)

或者,如果您不想使用嵌套函数:

def yaml_files_for_test(path, mocker, content1, content2):
    contents = {"file1.yaml": content1,
                "file2.yaml": content2}
    data = contents[os.path.basename(path)]
    mock = mocker.mock_open(read_data=yaml.dump(data))
    return mock.return_value


def test_open_func5(mocker, file1_content, file2_content):
    mocker.patch("pathlib.Path.open",
                 lambda path: yaml_files_for_test(path, mocker,
                                                  file2_content, file2_content))
    conf = open_func()
    assert isinstance(conf, dict)