2017-06-30 13 views
2

私がテストを実行するためにnosetestsを使用していますが、私はそれが本当のUnicodeを印刷できないことがわかります。nosetestsでユニコードを読み取り可能にする方法は?

#!/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 << ---------------------- 

答えて

0

-sまたは--nocaptureオプションを指定すると、nosetestは標準出力をキャプチャできなくなります。あなたが好きな文字列が表示されますが、print statemnetとして>> beging/end captured stdout<<マーカーなしで、すぐにそれが実行されますよう、文字列を出力します:

$ 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) 
関連する問題