0

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?

  • There is no good way to control that behavior, unfortunately. The code that's doing the shortening is [here](https://github.com/python/cpython/blob/a31f4cc881992e84d351957bd9ac1a92f882fa39/Lib/unittest/util.py#L24). 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. – John Szakmeister Feb 12 '19 at 11:40
  • I find it hard to believe that somebody thought that was sensible default functionality for the diff. Am I really the first one who finds this not really fit for purpose? – R. Kolosovs Feb 12 '19 at 13:28
  • No, you are not. This feature was added as part of issue [#18996](https://bugs.python.org/issue18996)--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](https://bugs.python.org/issue21820) was opened as a result. Python doesn't have a lot of paid contributors so, if this is something that bugs you, please help them by formulating and contributing a fix. They're a very welcoming community. :-) – John Szakmeister Feb 12 '19 at 14:00
  • Ok, thank you John. Can you put your last comment referencing the issues as an answer so I can accept it? I think that given the situation it is the best information and action advice regarding my problem. – R. Kolosovs Feb 13 '19 at 07:03

1 Answers1

0

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.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79