温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

TypeError: unicode argument expected, got 'str'

发布时间:2020-08-13 19:51:28 来源:网络 阅读:12852 作者:许大树 栏目:开发技术

今天在做mock模块中的patch()方法只在运行测试的上下文中才替换对象时,使用了io.StringIO结果出现报错:

TypeError: unicode argument expected, got 'str'


经确认是字符集的问题,考虑使用io.BytesIO解决了此问题


具体代码如下:

# -*- coding:utf-8 -*-

#
# from io import StringIO
from io import BytesIO
from unittest import TestCase
from mock import patch
import url

class TestUrlPrint(TestCase):
    def test_url_gets_to_stdout(self):
        protocol = 'http'
        host = 'www'
        domain = 'example.com'
        expected_url = '{}://{}.{}\n'.format(protocol, host, domain)

        with patch('sys.stdout', new=BytesIO()) as fake_out:
            url.urlprint(protocol, host, domain)
            self.assertEqual(fake_out.getvalue(), expected_url)


python2.7的字符转换问题,需要多加注意。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI