没有指定文件时测试失败

2024-05-23 20:25:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试一些python代码,当我用指定的文件运行nosetests时,一切正常,但是当我想运行文件夹中的所有东西时,一些测试(大多数)失败了。你知道吗

我在python2.7中使用mock、unittest和nose

谢谢

例如:

AssertionError: Expected call: mock('fake/path')
Not called

在这个测试中

def test_vm_exists(self):
    fake_path = 'fake/path'
    os.path.exists = mock.MagicMock()
    os.path.exists.return_value = True

    response = self._VixConnection.vm_exists(fake_path)

    os.path.exists.assert_called_with(fake_path)
    self.assertEqual(response, True)

这是回购: https://github.com/trobert2/nova-vix-driver/tree/unittests/vix/tests

如果描述得不够详细,我很抱歉。你知道吗


Tags: 文件path代码self文件夹trueosresponse
1条回答
网友
1楼 · 发布于 2024-05-23 20:25:13

实际上,您并不是在模仿实际代码使用的os.path.exists。请尝试以下操作:

@mock.patch('os.path.exists')
def test_vm_exists(self, mock_exists):
    mock_exists.return_value = True
    fake_path = 'fake/path'

    response = self._VixConnection.vm_exists(fake_path)

    mock_exists.assert_called_with(fake_path)
    self.assertEqual(response, True)

相关问题 更多 >