Open qingqi opened 8 years ago
So I have a solution for this encode/decode error.
Edit your validators.py as below:
def validate(self, body=None, headers=None, context=None):
reload(sys)
sys.setdefaultencoding('utf-8')
try:
extracted_val = self.extractor.extract(
body=body, headers=headers, context=context)
so
reload(sys)
sys.setdefaultencoding('utf-8')
is the key for output displayed correctly.
Reference: http://wangye.org/blog/archives/629/
There are multiple occurrence of string.format method. And when the method is called with unicode object, UnicodeEncodeError would be thrown.
Traceback (most recent call last): File "resttest.py", line 918, in
command_line_run(sys.argv[1:])
File "resttest.py", line 914, in command_line_run
main(args)
File "resttest.py", line 856, in main
failures = run_testsets(tests)
File "resttest.py", line 657, in run_testsets
result = run_test(test, test_config=myconfig, context=context, curl_handle=curl_handle)
File "resttest.py", line 412, in run_test
body=body, headers=head, context=my_context)
File "/Users/baidu/workspace/git/pyresttest/pyresttest/validators.py", line 386, in validate
self.comparator_name, extracted_val, expected_val)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
Sample cases:
sample response: {"status":0,"msg":"","data":{"result":[{"id":100972,"title":"PHILIPS 飞利浦 58PFL3740/T3 全高清LED液晶电视(黑色) 58英寸","subTitle":"","shortReason":"逐点高清技术,多接口多格式支持,性价比高。","price":"4099.0","priceHighlight":"历史最低","categoryId":1000569,"categoryName":"LED电视","merchantId":1,"merchantName":"京东商城","rejectReason":"","auditStatus":5}]}}