繁体   English   中英

如何使鼻子测试打印Unicode可读?

[英]How to make nosetests print unicode readable?

我正在使用nosetests测试进行测试,但是我发现它无法打印真正的unicode:

#!/usr/bin/env python
# encoding: utf-8

import unittest

class FooTests(unittest.TestCase):
    def test_str(self):
        print("中国")
        self.assertEqual(1, 0)

    def test_unicode(self):
        print(u"中国")
        self.assertEqual(1, 0)

def main():
    unittest.main()

if __name__ == "__main__":
    main()

其捕获结果如下:

-------------------- >> begin captured stdout << ---------------------
\u4e2d\u56fd

--------------------- >> end captured stdout << ----------------------

我想要的是:

-------------------- >> begin captured stdout << ---------------------
中国

--------------------- >> end captured stdout << ----------------------

指定-s--nocapture选项将防止nosetest捕获标准输出; 您将看到所需的字符串,但没有>> beging/end captured stdout<<标记,因为print statemnet将在执行该字符串后立即打印该字符串:

$ nosetests -s t.py
中国
F中国
F
======================================================================
FAIL: test_str (t.FooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/t.py", line 9, in test_str
    self.assertEqual(1, 0)
AssertionError: 1 != 0

======================================================================
FAIL: test_unicode (t.FooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/t.py", line 13, in test_unicode
    self.assertEqual(1, 0)
AssertionError: 1 != 0

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)

另一个选择:使用python 3! 不需要任何选项:

$ python3 -m nose t.py
FF
======================================================================
FAIL: test_str (t.FooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/t.py", line 9, in test_str
    self.assertEqual(1, 0)
AssertionError: 1 != 0
-------------------- >> begin captured stdout << ---------------------
中国

--------------------- >> end captured stdout << ----------------------

======================================================================
FAIL: test_unicode (t.FooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/t.py", line 13, in test_unicode
    self.assertEqual(1, 0)
AssertionError: 1 != 0
-------------------- >> begin captured stdout << ---------------------
中国

--------------------- >> end captured stdout << ----------------------

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

FAILED (failures=2)

我有类似的问题,可能是相同的根本原因。 如果使用LC_CTYPE=C nosetests运行测试,则LC_CTYPE=C nosetests测试Python不能从ASCII编码中编码出Unicode字符,因为语言环境决定了C 运行带有LC_CTYPE=en_US.UTF-8 nosetest应该可以解决该问题。

PS我正在寻找解决方案,如何在鼻子测试跑步者的测试中指出这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM