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

yipeiwu_com5年前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命令行库click的使用

一、前言 在本系列前面几篇文章中,我们分别介绍了 argparse 和 docopt 的主要功能和用法。它们各具特色,都能出色地完成命令行任务。argparse 是面向过程的,需要先设...

Python星号*与**用法分析

Python星号*与**用法分析

本文实例分析了Python星号*与**用法。分享给大家供大家参考,具体如下: 1. 加了星号(*)的变量名会存放所有未命名的变量参数,不能存放dict,否则报错。 如: def mu...

Python采集猫眼两万条数据 对《无名之辈》影评进行分析

Python采集猫眼两万条数据 对《无名之辈》影评进行分析

一、说明 本文主要讲述采集猫眼电影用户评论进行分析,相关爬虫采集程序可以爬取多个电影评论。 运行环境:Win10/Python3.5。 分析工具:jieba、wordcloud、pyec...

Django中ajax发送post请求 报403错误CSRF验证失败解决方案

Django中ajax发送post请求 报403错误CSRF验证失败解决方案

前言 今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败;先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了C...

python逐行读取文件内容的三种方法

方法一:复制代码 代码如下:f = open("foo.txt")          ...