TypeError: a bytes-like object is required, not 'str' 报这个错怎么解决啊?

来源:6-10 unittest接口测试实战生成测试报告

褚志鹏

2021-01-06

#coding = utf-8
import requests
import unittest
import sys
import os
import re
base_path = os.getcwd()
sys.path.append(base_path)
import json
import mock
import HtmlTestRunner
import htmlrunner
from Base.base_request import request

def read_json():
with open(base_path+’/Config/user_data.json’,‘rb’) as f:
data = json.load(f,encoding=‘utf-8’)
return data

def get_value(key):
data = read_json()
return data[key]

host = 'http://www.imooc.com/'
class ImoocCase(unittest.TestCase):
def test_activity(self):
url = host + 'activity/newcomer’
data = {
“timestamp”:“1561269343481”,
“uid”:“7213561”,
“token”:“7ad09430cbaf927af642ab843ec374ef”,
“type”:“1”,
“marking”:“androidbanner”,
“uuid”:“41b650ef846688193728ff7381eb6c1c”,
“secrect”:“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWUiOiI3MjEzNTYxIiwianRpIjoiM2I2NDg0NjQ2Nzk4NjI3NzU1YjRmZWE0ODliMDNmNmUiLCJkZXZpY2UiOiJtb2JpbGUifQ.EvGIFSHhij4lgEMdCtotFoTMtWSJLwVvridsoaWzdZY”
}
mock_method = mock.Mock(return_value=get_value(‘activity/newcomer’))
request.run_main = mock_method
res = request.run_main(‘post’,url,data)
print(res)
self.assertEqual(res[“errorCode”],1000)

def test_search(self):
    url = host + 'search/hotwords'
    data = {
        "timestamp":"1561269343481",
        "uid":"7213561",
        "token":"7ad09430cbaf927af642ab843ec374ef",
        "type":"1",
        "marking":"androidbanner",
        "uuid":"41b650ef846688193728ff7381eb6c1c",
        "secrect":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWUiOiI3MjEzNTYxIiwianRpIjoiM2I2NDg0NjQ2Nzk4NjI3NzU1YjRmZWE0ODliMDNmNmUiLCJkZXZpY2UiOiJtb2JpbGUifQ.EvGIFSHhij4lgEMdCtotFoTMtWSJLwVvridsoaWzdZY"
    }

    mock_method = mock.Mock(return_value=get_value('search/hotwords'))
    request.run_main = mock_method
    res = request.run_main('post',url,data)
    print(res)
    self.assertEqual(res["errorCode"],1000)

def default(self,obj):
    if isinstance(obj,bytes):
        return str(obj,encoding='utf-8')
    return json.JSONEncoder.default(self,obj)

if name == “main”:
suite = unittest.TestSuite()
suite.addTest(ImoocCase(“test_activity”))
suite.addTest(ImoocCase(“test_search”))
file_path = base_path +’/Report/report.html’
with open(file_path,‘wb’) as f:
f.write(bytes(file_path,encoding=‘utf-8’))
runner = HtmlTestRunner.HTMLTestRunner(stream=f,report_title=“this is test”,descriptions=“ceshi test”,verbosity=2)
runner.run(suite)
f.close()
# unittest.main()

PS F:\ImoocRequests> & C:/Users/Administrator/AppData/Local/Programs/Python/Python36/python.exe f:/ImoocRequests/UnittestCase/imooc_case.py
Traceback (most recent call last):
File “f:/ImoocRequests/UnittestCase/imooc_case.py”, line 76, in
runner.run(suite)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\HtmlTestRunner\runner.py”, line 67, in run
self.stream.writeln()
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\unittest\runner.py”, line 26, in writeln
self.write(’\n’) # text-mode streams translate to \r\n if needed
TypeError: a bytes-like object is required, not ‘str’

写回答

1回答

Mushishi

2021-01-06

你这个建议百度哦。这个是字节。你把字写转换成str

0
0

Python接口自动化测试框架实战 从设计到开发

接口自动化测试框架的设计与开发,并教你如何开发桩服务

1720 学习 · 831 问题

查看课程