PyCharm show full diff when unittest fails for multiline string?

后端 未结 2 1350
天命终不由人
天命终不由人 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..

提交回复
热议问题