Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm show full diff when unittest fails for multiline string?

I am writing some Python unit tests using the "unittest" framework and run them in PyCharm. Some of the tests compare a long generated string to a reference value read from a file. If this comparison fails, I would like to see the diff of the two compared strings using PyCharms diff viewer.

So the the code is like this:

    actual = open("actual.csv").read()
    expected = pkg_resources.resource_string('my_package', 'expected.csv').decode('utf8')
    self.assertMultiLineEqual(actual, expected)

And PyCharm nicely identifies the test as a failure and provides a link in the results window to click which opens the diff viewer. However, due to how unittest shortens the results, I get results such as this in the diff viewer:

Left side:

'time[57 chars]ercent 0;1;1;1;1;1;1;1 0;2;1;3;4;2;3;1 0;3;[110 chars]32 '

Right side:

'time[57 chars]ercen 0;1;1;1;1;1;1;1 0;2;1;3;4;2;3;1 0;3;2[109 chars]32 '

Now, I would like to get rid of all the [X chars] parts and just see the whole file(s) and the actual diff fully visualized by PyCharm.

I tried to look into unittest code but could not find a configuration option to print full results. There are some variables such as maxDiff and _diffThreshold but they have no impact on this print.

Also, I tried to run this in py.test but there the support in PyCharm was even less (no links even to failed test).

Is there some trick using the difflib with unittest or maybe some other tricks with another Python test framework to do this?

like image 753
kg_sYy Avatar asked May 08 '15 08:05

kg_sYy


People also ask

Is PyUnit the same as unittest?

PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)

Do Python UnitTests run in parallel?

By default, unittest-parallel runs unit tests on all CPU cores available.

Is Pytest faster than unittest?

The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class. But for pytest, we only have to define the testing function. Pytest is also fast and efficient.

How do I run unittest python in PyCharm?

If no specific test runner is installed, PyCharm uses unittest. To explicitly set the required test runner in the project settings, press Ctrl+Alt+S to open the IDE settings and select Tools | Python Integrated Tools, and then select the target test runner from the Default test runner list.


1 Answers

The TestCase.maxDiff=None answers given in many places only make sure that the diff shown in the unittest output is of full length. In order to also get the full diff in the <Click to see difference> link you have to set MAX_LENGTH.

import unittest

# Show full diff in unittest
unittest.util._MAX_LENGTH=2000

Source: https://stackoverflow.com/a/23617918/1878199

like image 142
ootwch Avatar answered Oct 14 '22 06:10

ootwch