测试积点老人 发表于 2020-6-9 10:47:22

执行测试用例后,报错了,不知道哪里出问题,有大神帮忙看一下不?

执行测试用例后,报错了,不知道哪里出问题,有大神帮忙看一下不?

import requests
import unittest
import os
import time
import sys
from HTMLTestRunner import HTMLTestRunner


class AddFile(unittest.TestCase):

    def addFile(self, pid, cname):
      url = 'https://webapi.115.com/files/add'
      header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 115Browser/13.0.0'}
      cookie = {
            'Cookie': 'acw_tc=784e2ca715906270849083403e4526dea21825ed693b2174d77b4f052244d4; UID=593103455_A1_1590628294; CID=a372b3a2a4835637db65773b5d003817; SEID=f9308c546fe8c23268964cd7f3ff4bf020abb4ef7f7419b3c4b662761e7279ac041058ad29e78ebe719f0bc1600c16c564e6eb6df5bce9f93dd996b9'
      }
      param = {'pid': pid, 'cname': cname}

      resp = requests.post(url=url, headers=header, cookies=cookie, data=param)
      result = resp.json()
      # print(result)
      if result['state'] is True:
            self.assertIs(result['state'], True)
            print(result['state'], '添加文件夹成功!')
      else:
            self.assertIs(result['state'], False)
            print(result['error'])

    # 测试添加的文件夹名称已存在,添加失败
    def test_addFile1(self):
      self.addFile(0, '魂牵梦萦')

    # 测试添加的文件夹名称不存在,可以正常添加
    def test_addFile2(self):
      self.addFile(0, '通过接口自动化添加的文件夹')


if __name__ == '__main__':
    # 构造测试集
    suite = unittest.TestSuite()
    # 添加测试用例
    suite.addTest(AddFile('test_addFile1'))
    suite.addTest(AddFile('test_addFile2'))

    # 获取当前执行时间
    # date = time.strftime('%Y-%m-%d')
    time = time.strftime('%Y-%m-%d %H-%M-%S')
    # 执行测试
    path = './report/'
    # 判断定义的测试报告保存路径是否存在,不存在则创建
    if not os.path.exists(path):
      os.makedirs(path)
    else:
      pass
    # 定义文件保存名字
    filename = path + time + '_report.html'

    report_title = u'测试报告'
    desc = u'接口自动化测试报告详情:'

    with open(filename, 'wb') as report:
      runner = HTMLTestRunner.HTMLTestRunner(stream=report, title=report_title, description=desc)
      runner.run(suite)

    # 关闭report
    report.close()
    # unittest.main()

运行后报错:

"D:\Program Files\Python\Python37\python.exe" E:/study/python/115Interface/addFile.py
..Traceback (most recent call last):
File "E:/study/python/115Interface/addFile.py", line 64, in <module>
    runner.run(suite)
File "D:\Program Files\Python\Python37\lib\site-packages\HTMLTestRunner\HTMLTestRunner.py", line 631, in run
    self.generateReport(test, result)
File "D:\Program Files\Python\Python37\lib\site-packages\HTMLTestRunner\HTMLTestRunner.py", line 691, in generateReport
    self.stream.write(output)
TypeError: a bytes-like object is required, not 'str'

Process finished with exit code 1


bellas 发表于 2020-6-10 09:29:15

ui自动化把这是

海海豚 发表于 2020-6-10 10:57:13

https://www.fujieace.com/python/str-bytes.html 参考下

jingzizx 发表于 2020-6-10 12:19:55

报错类型不对
页: [1]
查看完整版本: 执行测试用例后,报错了,不知道哪里出问题,有大神帮忙看一下不?