简体   繁体   English

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

[英]How to make nosetests print unicode readable?

I am using nosetests to run test, but I find that it cannot print the real unicode: 我正在使用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()

its captured result is like this: 其捕获结果如下:

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

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

What I want is: 我想要的是:

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

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

Specifying -s or --nocapture option will prevents nosetest to capture standard output; 指定-s--nocapture选项将防止nosetest捕获标准输出; You will see the string as you want, but without >> beging/end captured stdout<< marker as the print statemnet will print the string as soon as it's executed: 您将看到所需的字符串,但没有>> 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)

Another options: Use python 3! 另一个选择:使用python 3! No option is required: 不需要任何选项:

$ 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)

I have similar problem with probably same root cause. 我有类似的问题,可能是相同的根本原因。 If you run tests with LC_CTYPE=C nosetests Python unable to encode unicode characters out of ASCII encoding since locale dictates C . 如果使用LC_CTYPE=C nosetests运行测试,则LC_CTYPE=C nosetests测试Python不能从ASCII编码中编码出Unicode字符,因为语言环境决定了C Running with something LC_CTYPE=en_US.UTF-8 nosetest should fix that. 运行带有LC_CTYPE=en_US.UTF-8 nosetest应该可以解决该问题。

PS I'm looking for solution how to indicate this in test itself of nosetest runner. PS我正在寻找解决方案,如何在鼻子测试跑步者的测试中指出这一点。

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

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