laffra / auger

Automated Unittest Generation for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mocks can have incorrect return values

NextDesign1 opened this issue · comments

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.