Search code examples
pythonunit-testingdictionarydifftest-results

How to configure diff window in python unittest output?


I am testing my application with the inbuilt unittest library (python 3.5) and some of the test cases compare (lists of) dictionaries. When those tests fail the output is not very helpful:

First differing element 1:
{'emi[1557 chars]al': 509201.03, 'remaining_time': None, 'nomin[1213 chars]alse}
{'emi[1557 chars]al': '509,201.03', 'remaining_time': None, 'no[1218 chars]alse}

It is easy enough to see which list element is wrong but the limited diff window cuts of the key name of the differing dictionary entry. I know that I can show the full diff with self.maxDiff = None but that's not what I want. I like the limited diff window I just don't like where in the diff it is placed.

Can I configure the placement of the diff window somehow? Alternatively how can I get an informative test failure report with python tests when comparing dictionaries?


Solution

  • This feature was added as part of issue #18996--and I can see why... trying to find the difference in a long sequence of characters is definitely not fun. Someone else complained about the lack of control over the shortening in the issue and issue #21820 was opened as a result.

    The code that's doing the shortening is here. You might be able to monkey patch it in there, but I don't recommend doing such things. Alternatively, you can write your own method or extend the TestCase class to compare the list of dictionaries and have full control over the results too.