Python 在路径对象上模拟多个项目时出现问题

Python 在路径对象上模拟多个项目时出现问题,python,python-3.x,unit-testing,mocking,Python,Python 3.x,Unit Testing,Mocking,我有如下代码: @patch.object(Path, "__init__", return_value=None) @patch.object(Path, "exists", return_value=True) @patch.object(Path, "read_text", return_value="FAKEFIELD: 'FAKEVALUE'") def test_load_param_from_file_exists(self, *mock_path): expected_d

我有如下代码:

@patch.object(Path, "__init__", return_value=None)
@patch.object(Path, "exists", return_value=True)
@patch.object(Path, "read_text", return_value="FAKEFIELD: 'FAKEVALUE'")
def test_load_param_from_file_exists(self, *mock_path):
    expected_dict = YAML.safe_load("FAKEFIELD: 'FAKEVALUE'")
    return_dict = load_parameters("input", None)
    self.assertTrue(return_dict["FAKEFIELD"] == expected_dict["FAKEFIELD"])
加载\u参数的代码中,代码如下所示:

file_path = Path(parameters_file_path)
if file_path.exists():
    file_contents = file_path.read_text()
    return YAML.safe_load(file_contents)
@patch.object(Path, "__init__", return_value=None)
@patch.object(Path, "exists", return_value=False)
@patch.object(Path, "read_text", return_value="FAKEFIELD: 'FAKEVALUE'")
def test_load_param_from_file(self, mock_path, *mock_path_other):
    with self.assertRaises(ValueError):
        load_parameters("input", False)

    mock_path.read_text.return_value = "FAKEFIELD: 'FAKEVALUE'"
    expected_dict = YAML.safe_load("FAKEFIELD: 'FAKEVALUE'")

    return_dict = load_parameters("input", None)

    self.assertTrue(return_dict["FAKEFIELD"] == expected_dict["FAKEFIELD"])
现在,我必须把它分成两个测试,因为我似乎无法得到一个模拟对象,它允许我在“文件存在”和“文件不存在”之间切换。理想情况下,我可以做这样一个测试:

file_path = Path(parameters_file_path)
if file_path.exists():
    file_contents = file_path.read_text()
    return YAML.safe_load(file_contents)
@patch.object(Path, "__init__", return_value=None)
@patch.object(Path, "exists", return_value=False)
@patch.object(Path, "read_text", return_value="FAKEFIELD: 'FAKEVALUE'")
def test_load_param_from_file(self, mock_path, *mock_path_other):
    with self.assertRaises(ValueError):
        load_parameters("input", False)

    mock_path.read_text.return_value = "FAKEFIELD: 'FAKEVALUE'"
    expected_dict = YAML.safe_load("FAKEFIELD: 'FAKEVALUE'")

    return_dict = load_parameters("input", None)

    self.assertTrue(return_dict["FAKEFIELD"] == expected_dict["FAKEFIELD"])
需要明确的是,上面的方法不起作用,因为每个修补对象的实例化方式不同,当调用
load\u parameters
方法中的
Path
对象时,
exists
被正确模拟,但
read\u text
不返回任何值


我做错了什么?有没有办法在单个对象或类上修补多个方法

我认为你让事情变得比实际需要的更复杂了:

def test_load_param_from_file_exists(self):

    # Adjust the name as necessary
    mock_path = Mock()
    mock_path.exists.return_value = True
    mock_path.read_text.return_value = '{"FAKEFIELD": "FAKEVALUE"}'

    with patch("Path", return_value=mock_path):
        return_dict = load_parameters("input", None)

    self.assertTrue(return_dict["FAKEFIELD"] == 'FAKEVALUE')
Mock
配置为像您希望的
file\u path
那样运行,然后修补
path
以在调用该对象时返回该对象


(我删除了涉及环境变量的代码,因为在修补
Path
时,值并不明显重要)

有趣!我想我误解了Mock-这看起来真的很优雅。今天晚些时候我会检查答案!隐马尔可夫模型。。好的,当我这样做时,我得到-Traceback(最近一次调用last):AttributeError:type对象“Path”没有属性“rsplit”–中的文件“/tests/test_main.py”,第293行