python使用HTMLTestRunner导出饼图分析报告的方法

yipeiwu_com6年前Python基础

目录如下:

这里有使用

HTMLTestRunner和 echarts.common.min.js文件[见百度网盘,这里给自己留个记录便于查询]

unit_test.py代码如下:

import unittest
import requests
import time
import os.path
from common import HTMLTestRunner
 
 
class TestLogin(unittest.TestCase):
 
 def setUp(self):
  # 获取session对象
  self.session = requests.session()
  # 登录url
  self.url = 'http://XXXXXX/oauth/oauth/token'
 
 def test_login_success(self):
  data = {
   'grant_type': 'password',
   'username': 'iu',
   'password': '111',
   'client_id': 'web',
   'client_secret': 'web-secret'
  }
  r = self.session.post(url=self.url, data=data)
  try:
   self.assertEqual(r.json()['token_type'])
  except AssertionError as e:
   print(e)
  
 
 def test_username_not_exit(self):
  data = {
   'grant_type': 'password',
   'username': '322u',
   'password': '8',
   'client_id': 'web',
   'client_secret': 'web-secret'
  }
  r = self.session.post(url=self.url, data=data)
  try:
   self.assertEqual("用户名或密码错误", r.json()["error_description"])
  except AssertionError as e:
   print(e)
 
 def test_password_error(self):
  data = {
   'grant_type': 'password',
   'username': '2u',
   'password': '888ssss888',
   'client_id': 'web',
   'client_secret': 'web-secret'
  }
  r = self.session.post(url=self.url, data=data)
  try:
   self.assertEqual("用户名或密码错误", r.json()["error_description"])
  except AssertionError as e:
   print(e)
 
 def tearDown(self):
  self.session.close()
 
 
if __name__ == '__main__':
 # unittest.main()
 test = unittest.TestSuite()
 test.addTest(TestLogin('test_login_success'))
 test.addTest(TestLogin('test_username_not_exit'))
 test.addTest(TestLogin('test_password_error'))
 
 rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
 file_path = os.path.abspath('.') + '\\report\\' + rq + '-result.html'
 
 file_result = open(file_path, 'wb')
 
 runner = HTMLTestRunner.HTMLTestRunner(stream=file_result, title=u'测试报告', description=u'用例执行情况')
 runner.run(test)
 file_result.close()

运行产生报告查看报告:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用requests提交HTTP表单的方法

Python的requests库, 其口号是HTTP for humans,堪称最好用的HTTP库。 使用requests库,可以使用数行代码实现自动化的http操作。以http pos...

Python 类,property属性(简化属性的操作),@property,property()用法示例

本文实例讲述了Python 类,property属性(简化属性的操作),@property,property()用法。分享给大家供大家参考,具体如下: property属性的创建方式有两...

python 实现多线程下载视频的代码

python 实现多线程下载视频的代码

代码: def thread(url): r = requests.get(url, headers=None, stream=True, timeout=30) # pri...

使用Numpy读取CSV文件,并进行行列删除的操作方法

Numpy是Python强大的数据计算和处理模块,其操作数据非常迅速且简单易行。 首先读取CSV文件 >>> import numpy as np#导入numpy模...

原来我一直安装 Python 库的姿势都不对呀

平常我都是直接执行 pip install 安装的第三方库,很多教程也是这么介绍的,一直以来我都认为这是标准的、正确的安装 Python 第三方库的姿势。直到我最近看到Python核心开...