PyCharm show full diff when unittest fails for multiline string?

后端 未结 2 1351
天命终不由人
天命终不由人 2021-02-15 09:10

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

相关标签:
2条回答
  • 2021-02-15 09:43

    Well, I managed to hack myself around this for my test purposes. Instead of using the assertEqual method from unittest, I wrote my own and use that inside the unittest test cases. On failure, it gives me the full texts and the PyCharm diff viewer also shows the full diff correctly.

    My assert statement is in a module of its own (t_assert.py), and looks like this

    def equal(expected, actual):
        msg = "'"+actual+"' != '"+expected+"'"
        assert expected == actual, msg
    

    In my test I then call it like this

        def test_example(self):
            actual = open("actual.csv").read()
            expected = pkg_resources.resource_string('my_package', 'expected.csv').decode('utf8')
            t_assert.equal(expected, actual)
            #self.assertEqual(expected, actual)
    

    Seems to work so far..

    0 讨论(0)
  • 2021-02-15 10:02

    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

    0 讨论(0)
提交回复
热议问题