I am using nosetests
to run test, but I find that it cannot print the real 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; 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:
$ 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! 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
. Running with something LC_CTYPE=en_US.UTF-8 nosetest
should fix that.
PS I'm looking for solution how to indicate this in test itself of nosetest runner.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.