TypeError: a bytes-like object is required, not 'str'
来源:6-10 unittest接口测试实战生成测试报告
晓慕标219
2020-06-12
import sys
import os
base_path = os.getcwd()
print(base_path)
sys.path.append(base_path)
import unittest
from Base.base_request import request
import json
from unittest import mock
import HTMLTestRunner
def read_json():
with open(base_path + “/Config/user_data.json”) as f:
data = json.load(f)
return data
def get_value(key):
data = read_json()
return data[key]
host = ‘http://www.imooc.com/’
class ImoocCase(unittest.TestCase):
def test_banner(self):
url = host + ‘api3/getbanneradvertver2’
data = {
‘timestamp’:‘1591929429872’,
‘uid’:‘0’,
‘token’:‘5eeef41f99a265e1fd2af056da6af7a3’,
‘type’:‘1’,
‘marking’:‘androidbanner’,
‘uuid’:’’,
‘secrect’:’’
}
mock_method = mock.Mock(return_value=get_value(‘api3/getbanneradvertver2’))
request.run_main = mock_method
res = request.run_main(‘post’, url, data)
self.assertEqual(res[‘errorCode’], 1000)
def test_ads(self):
url = host + 'api3/getads'
data = {
'timestamp':'1591929429871',
'uid':'0',
'token':'95140a63f9778fb0304143b619c1ca53',
'type':'1',
'marking':'androidsa',
'uuid':'',
'secrect':''
}
mock_method = mock.Mock(return_value=get_value('api3/getads'))
request.run_main = mock_method
res = request.run_main('post', url, data)
self.assertEqual(res['errorCode'], 1000)
def test_newcourseskill(self):
url = host + 'api3/newcourseskill'
data = {
'timestamp':'1591929429876',
'uid':'0',
'token':'05f57c93245042a6dcebaf50215d82b6',
'uuid':'',
'secrect':''
}
mock_method = mock.Mock(return_value=get_value('api3/newcourseskill'))
request.run_main = mock_method
res = request.run_main('post', url, data)
self.assertEqual(res['errorCode'], 1000)
def test_downrecommend(self):
url = host + 'api3/getdownrecommend'
data = {
'timestamp':'1591929429880',
'uid':'0',
'token':'ecaf2b5fe8cc1761d77531c66449e426',
'uuid':'',
'secrect':''
}
mock_method = mock.Mock(return_value=get_value('api3/getdownrecommend'))
request.run_main = mock_method
res = request.run_main('post', url, data)
self.assertEqual(res['errorCode'], 1000)
def test_lastlearn(self):
url = host + 'api3/getlastlearn'
data = {
'timestamp':'1591929429874',
'uid':'0',
'token':'fba82dc3674e5718696e7208f265904d',
'uuid':'',
'secrect':''
}
mock_method = mock.Mock(return_value=get_value('api3/getlastlearn'))
request.run_main = mock_method
res = request.run_main('post', url, data)
self.assertEqual(res['errorCode'], 1000)
def test_beta4(self):
url = host + 'api3/beta4'
data = {
'timestamp':'1591929429873',
'uid':'0',
'token':'15bc08d0518bd93d6a70a33daa532579',
'uuid':'',
'secrect':''
}
mock_method = mock.Mock(return_value=get_value('api3/beta4'))
request.run_main = mock_method
res = request.run_main('post', url, data)
self.assertEqual(res['errorCode'], 1000)
if name == “main”:
suite = unittest.TestSuite()
suite.addTest(ImoocCase(‘test_banner’))
suite.addTest(ImoocCase(‘test_ads’))
suite.addTest(ImoocCase(‘test_newcourseskill’))
suite.addTest(ImoocCase(‘test_downrecommend’))
suite.addTest(ImoocCase(‘test_lastlearn’))
suite.addTest(ImoocCase(‘test_beta4’))
file_path = base_path + '/Report/report.html'
with open(file_path, 'wb') as f:
title = "自动化测试报告"
title.encode()
runner = HTMLTestRunner.HTMLTestRunner(stream=f, title=title, description="ChenXiaoQing test")
runner.run(suite)
f.close()
# unittest.main()
PS E:\ImoocInterface> & D:/Python/Python38/python.exe e:/ImoocInterface/UnittestCase/imooc_case.py
E:\ImoocInterface
FF…F.Traceback (most recent call last):
File “e:/ImoocInterface/UnittestCase/imooc_case.py”, line 124, in
runner.run(suite)
File “D:\Python\Python38\lib\HTMLTestRunner.py”, line 582, in run
self.generateReport(test, result)
File “D:\Python\Python38\lib\HTMLTestRunner.py”, line 642, in generateReport
self.stream.write(output)
TypeError: a bytes-like object is required, not ‘str’
老师这个要怎么处理呢?
2回答
-
兴风作浪的小哥哥
2020-07-28
self.stream.write(output.encode('utf-8')) 这个是在HTMLTestRunner.py文件下修改的吗
012020-07-28 -
晓慕标219
提问者
2020-06-12
self.stream.write(output.encode('utf-8'))
已解决,老师看下这样没错吧,可以正常执行并且生成测试报告
012020-06-12
相似问题