Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocks can have incorrect return values #14

Open
NextDesign1 opened this issue Nov 10, 2018 · 0 comments
Open

Mocks can have incorrect return values #14

NextDesign1 opened this issue Nov 10, 2018 · 0 comments
Labels

Comments

@NextDesign1
Copy link
Collaborator

NextDesign1 commented Nov 10, 2018

I've encountered an error where the return values from the generated mocks can be incorrect if the function is called multiple times. For example, given a file to test such as the following:

import os

def my_func(path):
    return os.path.dirname(path)

def main():
    print my_func('C:/temp')
    print my_func('/usr/tmp')
    print my_func('/usr/tmp/foo')

if __name__ == '__main__':
    main()

In some cases, it will generate the following, incorrect test.

class FunctionsTest(unittest.TestCase):
    @patch.object(posixpath, 'dirname')
    def test_func_four(self, mock_dirname):
        mock_dirname.return_value = 'C:'
        self.assertEqual(
            functions.func_four(path='/usr/tmp/foo'),
            '/usr/tmp'
        )

I believe that this is because of the two following lines:

args, return_value = list(mock.calls.values())[0][0]

self.dump_call(filename, code, random.choice(list(function.calls.values())))

Auger is choosing a random function call to generate a test case for, but is always choosing the first mock, regardless of the call's arguments.

I believe that there is a large advantage to generating test cases for each invocation of a function, especially when refactoring code bases, as you can ensure that a function is well exercised. I have something in my working copy that does this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant