• python中使用unittest+HTMLTestReportCN框架输出测试报告


    # encoding: utf-8
    #@author: lizheng
    #@file: test_create_tag_api_case.py
    #@time: 2021/3/15 13:51
    import unittest
    import requests
    import os
    import time
    import json
    import HTMLTestReportCN


    class Test_Weixei_case(unittest.TestCase):
    def setUp(self) -> None:
    self.session = requests.session()
    self.g = globals() #设置用户全局变量
    # self.file_path = os.path.join(os.path.dirname(__file__),'../../report')
    print('初始化')
    # print(self.file_path)

    def tearDown(self) -> None:
    self.session.close()

    def test_get_accesstoken_success(self): #获取微信公众号access_token
    self._testMethodName='case01'
    self._testMethodDoc = '验证获取access_token是否成功'
    get_accesstoken_data = {
    'grant_type':'client_credential',
    'appid':'个人微信appid',
    'secret':'******'
    }
    response = self.session.get(url='https://api.weixin.qq.com/cgi-bin/token',params=get_accesstoken_data) #请求路径和请求参数params
    actual_result = response.status_code #返回状态码获取
    self.assertEqual(actual_result,200,'调用成功') #断言处理是否成功
    print(response.content.decode('utf-8'))
    self.g['tokenid'] = response.json()['access_token'] #获取accesstoken作为下一个入参
    print(self.g['tokenid'])


    def test_get_accesstoken_fail(self): #获取accesstoken失败用例
    self._testMethodName = 'case02'
    self._testMethodDoc = '验证获取access_token是否成功'
    get_accesstoken_data_fail = {
    'grant_type': 'client_credential',
    'appid': '个人appid',
    'secret': '*******'
    }
    response = self.session.get(url='https://api.weixin.qq.com/cgi-bin/token', params=get_accesstoken_data_fail)
    actual_result = response.json()['errcode']
    self.assertEqual(actual_result, 40013, 'AppID无效错误')
    print('AppID无效错误')


    def test_get_user_list_success(self):
    self._testMethodName = 'case03'
    self._testMethodDoc = '验证获取用户列表是否成功'
    get_accessuser_data = {
    'ACCESS_TOKEN':self.g['tokenid'], #全局变量入参
    'next_openid': ''
    }
    response = self.session.get(url='https://api.weixin.qq.com/cgi-bin/user/get', params=get_accessuser_data)
    actual_result = response.status_code
    self.assertEqual(actual_result,200,'验证获取用户列表是否成功')
    print(response.content.decode('utf-8'))
    print('验证获取用户列表是否成功')



    def Suite():
    suiteTest = unittest.TestSuite()
    suiteTest.addTest(Test_Weixei_case("test_get_accesstoken_success"))
    suiteTest.addTest(Test_Weixei_case("test_get_accesstoken_fail"))
    suiteTest.addTest(Test_Weixei_case("test_get_user_list_success"))
    return suiteTest







    if __name__=='__main__':
    filename = 'test_' + time.strftime('%y_%m_%d_%H_%M_%S')+'.html' #生成报告格式
    file_path = os.path.join(os.path.dirname(__file__),'../../report/%s'%filename)
    fp = open(file_path, 'wb')
    runner = HTMLTestReportCN.HTMLTestRunner(stream=fp, title='自动化测试报告', # description='详细测试用例结果', #不传默认为空
    description='执行测试用例报告',
    tester='***'# 测试人员名字,不传默认为QA
    )
    #运行测试用例
    runner.run(Suite())
    print(file_path)
       # 关闭文件,否则会无法生成文件
    fp.close()

  • 相关阅读:
    laravel使用redis报错
    PHP新特性W3Cschool
    【python】跳过验证直接登陆-cookie已经知道需要部分直接注入
    【python】显示等待
    【python】pymysql库的简单使用
    【python】UI自动化优化浏览器重复打开和跑脚本时电脑无法做其他工作的问题
    【python】seleniumwire和selenium的区别
    【python】UI自动化-新版
    【python】UI自动化获取输入框的值
    【python】UI自动化多窗口处理
  • 原文地址:https://www.cnblogs.com/boosli/p/14543400.html
Copyright © 2020-2023  润新知