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中的for循环

python中的for循环

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。 语法: for循环的语法格式如下: for iterating_var in sequence:...

Python中计算三角函数之cos()方法的使用简介

 cos()方法返回x弧度的余弦值。 语法 以下是cos()方法的语法: cos(x) 注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象...

Flask框架单例模式实现方法详解

本文实例讲述了Flask框架单例模式实现方法。分享给大家供大家参考,具体如下: 单例模式: 程序运行时只能生成一个实例,避免对同一资源产生冲突的访问请求。 Django &nb...

python中count函数简单用法

python中count函数的用法 Python count()方法 描述 Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。...

详解Python3中的Sequence type的使用

其实本来是要reverse一下list的,就去查了一下list[::-1]是什么意思,发现还有很多要注意的地方,所以就记一下。 主要是参照https://docs.python.org/...