Question

I am testing some python code and when I`m running nosetests with the file specified, Everything is good, but when I want to run everything in the folder, some of the tests (most) fail.

I am using mock, unittest and nose with python 2.7

Thank you

for example:

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

on this test

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)

this is the repo: https://github.com/trobert2/nova-vix-driver/tree/unittests/vix/tests

Sorry if it wasn't descriptive enough.

Was it helpful?

Solution

You're not actually mocking os.path.exists as used by the actual code. Try the following:

@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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top