python - assertEqual - two identical lists, why I get strange result? -
i have unit tests:
import unittest class testfail(unittest.testcase): def testfail(self): info = range(5) self.assertequal(data, insertion_sorting(data)) class testsuccess(unittest.testcase): def testsuccess(self): info = range(5) self.assertequal([0,1,2,3,4], insertion_sorting(data)) def insertion_sorting(data): result = [] while len(data): min_index = 0 in range(len(data)): if data[i] < data[min_index]: min_index = result.append(data[min_index]) del data[min_index] homecoming result if __name__ == '__main__': unittest.main()
testsuccess ran successful, testfail raises:
assertionerror: lists differ: [] != [0, 1, 2, 3, 4]
second list contains 5 additional elements. first element 0: 0
[] [0, 1, 2, 3, 4]could explain me, why testsuccess ran successful, testfail not?
your insertion_sorting()
function destructive: modifies in-place list pass. therefore, list referenced data
variable defined in testfail()
indeed cleared during phone call insertion_sorting()
.
a simple workaround operate on re-create of list:
self.assertequal(data, insertion_sorting(data[:]))
a more complicated alternative refactor insertion_sorting()
not destructive.
python unit-testing
No comments:
Post a Comment