【问题标题】:Nosetests failing when no file is specified未指定文件时 Nosetests 失败
【发布时间】:2013-12-08 10:16:35
【问题描述】:

我正在测试一些 python 代码,当我使用指定的文件运行 nosetests 时,一切都很好,但是当我想运行文件夹中的所有内容时,一些测试(大多数)失败。

我在 python 2.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

如果描述性不够,请见谅。

【问题讨论】:

  • 欢迎来到stackoverflow。你能显示一些代码吗?什么失败,哪个异常,...请改进您的问题。
  • 我进行了编辑。感谢您的意见
  • 非常感谢!打补丁解决一切。当我使用 MagicMock 时,我认为它只能在测试范围内工作。我错了,谢谢。

标签: python unit-testing mocking nose


【解决方案1】:

您实际上并不是在模拟实际代码中使用的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)

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多