簡體   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