Python单元测试中多行字符串的比较 [英] Comparison of multi-line strings in Python unit test

查看:39
本文介绍了Python单元测试中多行字符串的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Python 单元测试中比较两个 Unicode 字符串时,它会给出一个很好的失败消息,突出显示哪些行和字符不同.但是,比较两个 8 位字符串只是显示两个字符串,没有突出显示.

When I compare two Unicode strings in a Python unit test, it gives a nice failure message highlighting which lines and characters are different. However, comparing two 8-bit strings just shows the two strings with no highlighting.

如何同时突出显示 Unicode 和 8 位字符串?

How can I get the highlighting for both Unicode and 8-bit strings?

以下是显示两种比较的示例单元测试:

Here is an example unit test that shows both comparisons:

import unittest

class TestAssertEqual(unittest.TestCase):
    def testString(self):
        a = 'xax\nzzz'
        b = 'xbx\nzzz'
        self.assertEqual(a, b)

    def testUnicode(self):
        a = u'xax\nzzz'
        b = u'xbx\nzzz'
        self.assertEqual(a, b)

if __name__ == '__main__':
    unittest.main()

这个测试的结果显示了差异:

The results of this test show the difference:

FF
======================================================================
FAIL: testString (__main__.TestAssertEqual)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/data/don/workspace/scratch/scratch.py", line 7, in testString
    self.assertEqual(a, b)
AssertionError: 'xax\nzzz' != 'xbx\nzzz'

======================================================================
FAIL: testUnicode (__main__.TestAssertEqual)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/data/don/workspace/scratch/scratch.py", line 12, in testUnicode
    self.assertEqual(a, b)
AssertionError: u'xax\nzzz' != u'xbx\nzzz'
- xax
?  ^
+ xbx
?  ^
  zzz

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=2)

Python 3 更新

在 Python 3 中,字符串文字默认是 Unicode,所以这几乎无关紧要.assertMultiLineEqual() 不再支持字节字符串,因此除非您愿意将字节字符串解码为 Unicode,否则您几乎无法使用常规的 assertEqual().>

Update for Python 3

In Python 3, string literals are Unicode by default, so this is mostly irrelevant. assertMultiLineEqual() no longer supports byte strings, so you're pretty much stuck with regular assertEqual() unless you're willing to decode the byte strings to Unicode.

推荐答案

Python 源代码 显示 TestCase 注册了一堆方法来测试不同类型的相等性.

A little digging in the Python source code shows that TestCase registers a bunch of methods to test equality for different types.

self.addTypeEqualityFunc(dict, 'assertDictEqual')
self.addTypeEqualityFunc(list, 'assertListEqual')
self.addTypeEqualityFunc(tuple, 'assertTupleEqual')
self.addTypeEqualityFunc(set, 'assertSetEqual')
self.addTypeEqualityFunc(frozenset, 'assertSetEqual')
try:
    self.addTypeEqualityFunc(unicode, 'assertMultiLineEqual')
except NameError:
    # No unicode support in this build
    pass

您可以看到 unicode 已注册使用 assertMultiLineEqual(),但 str 未注册任何特殊内容.我不知道为什么 str 被排除在外,但到目前为止,我对以下两种方法中的任何一种都感到满意.

You can see that unicode is registered to use assertMultiLineEqual(), but str is not registered for anything special. I have no idea why str is left out, but so far I have been happy with either of the following two methods.

如果一个 8 位的字符串默认没有注册使用 assertMultiLineEqual(),你仍然可以直接调用它.

If an 8-bit string isn't registered to use assertMultiLineEqual() by default, you can still call it directly.

def testString(self):
    a = 'xax\nzzz'
    b = 'xbx\nzzz'
    self.assertMultiLineEqual(a, b)

注册字符串类型

您也可以自己注册.只需在测试用例的 setUp() 方法中添加额外的一行.做一次,你所有的测试方法都会使用正确的方法来测试相等性.如果您的项目有一个用于所有测试用例的公共基类,那将是放置它的好地方.

Register String Type

You can also register it yourself. Just add an extra line to your test case's setUp() method. Do it once, and all your test methods will use the right method to test equality. If your project has a common base class for all test cases, that would be a great place to put it.

class TestAssertEqual(unittest.TestCase):
    def setUp(self):
        super(TestAssertEqual, self).setUp()
        self.addTypeEqualityFunc(str, self.assertMultiLineEqual)

    def testString(self):
        a = 'xax\nzzz'
        b = 'xbx\nzzz'
        self.assertEqual(a, b)

    def testUnicode(self):
        a = u'xax\nzzz'
        b = u'xbx\nzzz'
        self.assertEqual(a, b)

当字符串比较失败时,这些方法中的任何一种都将包括突出显示.

Either of these methods will include highlighting when the string comparison fails.

这篇关于Python单元测试中多行字符串的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆