PyCharm show full diff when unittest fails for multiline string?

后端 未结 2 1933
故里飘歌
故里飘歌 2021-02-15 09:04

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:58

    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..

提交回复
热议问题