TypeError: unicode argument expected, got 'str'
今天在做mock模块中的patch()方法只在运行测试的上下文中才替换对象时,使用了io.StringIO结果出现报错:
经确认是字符集的问题,考虑使用io.BytesIO解决了此问题
具体代码如下:
#-*-coding:utf-8-*-##fromioimportStringIOfromioimportBytesIOfromunittestimportTestCasefrommockimportpatchimporturlclassTestUrlPrint(TestCase):deftest_url_gets_to_stdout(self):protocol='http'host='www'domain='example.com'expected_url='{}://{}.{}\n'.format(protocol,host,domain)withpatch('sys.stdout',new=BytesIO())asfake_out:url.urlprint(protocol,host,domain)self.assertEqual(fake_out.getvalue(),expected_url)
python2.7的字符转换问题,需要多加注意。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。