最新下载地址:https://github.com/allure-framework/allure2/releases
配置环境变量:
2、使用[pytest] #addopts = --html=./report/report.html --reruns 3 --reruns-delay=2 #addopts = --html=./report/report.html -s addopts = -s --alluredir ./report/result testpaths = testcase python_files = test_*.py python_classes = Test* python_functions = test_*
生成测试报告:allure generate report/result -o report/html --clean
allure应用# -*- coding: utf-8 -*- # @Time : 2021/12/12 3:18 # @Author : jeffky # @File : test_excel_case.py import json from pprint import pprint import pytest as pytest from utils.AssertUtil import AssertUtil from common.base import * from utils.RequestsUtil import Request from common import ExcelConfig from utils.LogUtil import my_log from config.Conf import ConfigYaml, base_DIR from common.ExcelData import Data import os import allure # 1、初始化信息 # 1)初始化测试用例文件 conf_read = ConfigYaml() case_file =os.path.join((base_DIR+os.sep+"data"),conf_read.get_excel_file()) # print(case_file) # 2)测试用例sheet名称 sheet_name = conf_read.get_excel_sheet() # 3)获取是否都运行测试用例列表 data_init = Data(case_file,sheet_name) run_list = data_init.get_run_list() # 初始化data_conf data_key = ExcelConfig.DataConfig # 4)日志 log = my_log() # 2、测试用例方法,参数化运行 class TestExcel: # 1、增加pytest # 2、修改方法参数 # 3、重构函数内容 # 4、pytest.main def run_api(self,url,method,params=None,header=None,cookie=None): ''' 发送请求api :return: ''' request = Request() if len(str(params).strip()) is not 0: params=json.loads(params) # method post/getopt if str(method).lower()=='get': # 2、增加headers res = request.get(url,json=params,headers = header,cookies=cookie) elif str(method).lower()=='post': res = request.post(url,json=params,headers = header,cookies=cookie) else: log.error("错误请求method--->",method) return res def run_pre(self,pre_case): url = conf_read.get_conf_url()+pre_case[data_key.url] print("url---->",url) method = pre_case[data_key.method] params=pre_case[data_key.params] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 1、判断headers是否存在,json转义,无需 header =json_parse(headers) # 3、增加cookis cookie = json_parse(cookies) res =self.run_api(url, method, params, header, cookie) print('前置用例执行条件-----》',res) return res # 1)初始化信息,url,data # 1、增加pytest @pytest.mark.parametrize("case",run_list) def test_run(self,case): # data_key = ExcelConfig.DataConfig # run_list第1个用例,key获取values url = conf_read.get_conf_url()+case[data_key.url] print("url---->",url) case_id = case[data_key.case_id] case_model=case[data_key.case_model] case_name=case[data_key.case_name] pre_exec = case[data_key.pre_exec] method =case[data_key.method] params_type=case[data_key.params_type] params=case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify=case[data_key.db_verify] # 1、验证前置条件 if pre_exec: # 前置测试用例 pre_case = data_init.get_case_pre(pre_exec) pre_res = self.run_pre(pre_case) headers,cookies = self.get_correlation(headers,cookies,pre_res) # 1、判断headers是否存在,json转义,无需 header =json_parse(headers) # 3、增加cookis cookie = json_parse(cookies) # 2)接口请求 res = self.run_api(url, method, params, header, cookie) print("测试用例执行----->",res) # sheet名称 feature 一级标签 allure.dynamic.feature(sheet_name) # 模块 story 二级标签 allure.dynamic.story(case_model) # 用例ID+接口名称 title allure.dynamic.title(case_id+case_name) # 请求URL 请求类型 期望结果 实际结果描述 desc = "请求url:{}
" "请求类型:{}
" "期望结果:{}
" " 实际结果:{}".format(url,method,expect_result,res) allure.dynamic.description(desc) #断言验证 # 状态码, 返回结果内容,数据库相关结果验证 # 状态码 assert_util = AssertUtil() assert_util.assert_code(int(res['code']),int(code)) # 返回结果内容 assert_util.assert_in_body(str(res['body']),str(expect_result)) # 数据库相关结果验证 # 1、初始化数据库 from common.base import init_db # 2、查询sql,excel定义好的 if db_verify: # conn = init_db('db_1') # res_db = conn.fetchone(db_verify) # log.debug("数据库查询结果:{}".format(str(res_db))) # # assert_util.assert_body(res['body']['username'],res_db['username']) # # 3、数据库的结果与接口返回的结果验证 # # 获取数据库结果的key # verify_list = list(dict(res_db).keys()) # # 根据key获取数据库结果,接口结果 # for line in verify_list: # res_line = res['body'][line] # res_db_line = dict(res_db)[line] # # 验证 # assert_util.assert_body(res_line , res_db_line) assert_db("db_1",res['body'],db_verify) # 关联 def get_correlation(self,headers,cookies,pre_res): # 验证是否有关联 headers_para,cookies_para = params_find(headers,cookies) # 有关联执行前置用例,获取结果 if len(headers_para): headers_data = pre_res['body'][headers_para[0]] # 结果替换 headers = res_sub(headers,headers_data) if len(cookies_para): cookies_data = pre_res['body'][cookies_para[0]] # 结果替换 cookies= res_sub(cookies,cookies_data) return headers,cookies if __name__ == '__main__': pytest.main(['-s','test_excel_case.py']) #动态关联 # 1、验证前置条件 # 2、找到执行用例 # 3、发送请求,获取前置用例结果 # 发送获取前置测试用例,用例结果 # 数据初始化,get/post,重构 # 4、替换headers变量 # 5、请求发送
修改base
def allure_report(report_path,report_html): ''' 生成allure报告 :param report_path: :param report_html: :return: ''' allure_cmd='allure generate %s -o %s --clean'%(report_path,report_html) log.info("报告地址") try: subprocess.call(allure_cmd,shell=True) except: log.error("执行用例失败,请检查一下测试环境相关配置") raise
修改conf
#定义report目录的路径 _report_path = base_DIR+os.sep+"report" def get_report_path(): return _report_path
编辑测试用例
# -*- coding: utf-8 -*- # @Time : 2021/12/12 3:18 # @Author : jeffky # @File : test_excel_case.py import json from pprint import pprint import pytest as pytest from utils.AssertUtil import AssertUtil from common.base import * from utils.RequestsUtil import Request from common import ExcelConfig from utils.LogUtil import my_log from config.Conf import ConfigYaml, base_DIR, get_report_path from common.ExcelData import Data import os import allure # 1、初始化信息 # 1)初始化测试用例文件 conf_read = ConfigYaml() case_file =os.path.join((base_DIR+os.sep+"data"),conf_read.get_excel_file()) # print(case_file) # 2)测试用例sheet名称 sheet_name = conf_read.get_excel_sheet() # 3)获取是否都运行测试用例列表 data_init = Data(case_file,sheet_name) run_list = data_init.get_run_list() # 初始化data_conf data_key = ExcelConfig.DataConfig # 4)日志 log = my_log() # 2、测试用例方法,参数化运行 class TestExcel: # 1、增加pytest # 2、修改方法参数 # 3、重构函数内容 # 4、pytest.main def run_api(self,url,method,params=None,header=None,cookie=None): ''' 发送请求api :return: ''' request = Request() if len(str(params).strip()) is not 0: params=json.loads(params) # method post/getopt if str(method).lower()=='get': # 2、增加headers res = request.get(url,json=params,headers = header,cookies=cookie) elif str(method).lower()=='post': res = request.post(url,json=params,headers = header,cookies=cookie) else: log.error("错误请求method--->",method) return res def run_pre(self,pre_case): url = conf_read.get_conf_url()+pre_case[data_key.url] print("url---->",url) method = pre_case[data_key.method] params=pre_case[data_key.params] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 1、判断headers是否存在,json转义,无需 header =json_parse(headers) # 3、增加cookis cookie = json_parse(cookies) res =self.run_api(url, method, params, header, cookie) print('前置用例执行条件-----》',res) return res # 1)初始化信息,url,data # 1、增加pytest @pytest.mark.parametrize("case",run_list) def test_run(self,case): # data_key = ExcelConfig.DataConfig # run_list第1个用例,key获取values url = conf_read.get_conf_url()+case[data_key.url] print("url---->",url) case_id = case[data_key.case_id] case_model=case[data_key.case_model] case_name=case[data_key.case_name] pre_exec = case[data_key.pre_exec] method =case[data_key.method] params_type=case[data_key.params_type] params=case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify=case[data_key.db_verify] # 1、验证前置条件 if pre_exec: # 前置测试用例 pre_case = data_init.get_case_pre(pre_exec) pre_res = self.run_pre(pre_case) headers,cookies = self.get_correlation(headers,cookies,pre_res) # 1、判断headers是否存在,json转义,无需 header =json_parse(headers) # 3、增加cookis cookie = json_parse(cookies) # 2)接口请求 res = self.run_api(url, method, params, header, cookie) print("测试用例执行----->",res) # sheet名称 feature 一级标签 allure.dynamic.feature(sheet_name) # 模块 story 二级标签 allure.dynamic.story(case_model) # 用例ID+接口名称 title allure.dynamic.title(case_id+case_name) # 请求URL 请求类型 期望结果 实际结果描述 desc = "请求url:{}
" "请求类型:{}
" "期望结果:{}
" " 实际结果:{}".format(url,method,expect_result,res) allure.dynamic.description(desc) #断言验证 # 状态码, 返回结果内容,数据库相关结果验证 # 状态码 assert_util = AssertUtil() assert_util.assert_code(int(res['code']),int(code)) # 返回结果内容 assert_util.assert_in_body(str(res['body']),str(expect_result)) # 数据库相关结果验证 # 1、初始化数据库 from common.base import init_db # 2、查询sql,excel定义好的 if db_verify: # conn = init_db('db_1') # res_db = conn.fetchone(db_verify) # log.debug("数据库查询结果:{}".format(str(res_db))) # # assert_util.assert_body(res['body']['username'],res_db['username']) # # 3、数据库的结果与接口返回的结果验证 # # 获取数据库结果的key # verify_list = list(dict(res_db).keys()) # # 根据key获取数据库结果,接口结果 # for line in verify_list: # res_line = res['body'][line] # res_db_line = dict(res_db)[line] # # 验证 # assert_util.assert_body(res_line , res_db_line) assert_db("db_1",res['body'],db_verify) # 关联 def get_correlation(self,headers,cookies,pre_res): # 验证是否有关联 headers_para,cookies_para = params_find(headers,cookies) # 有关联执行前置用例,获取结果 if len(headers_para): headers_data = pre_res['body'][headers_para[0]] # 结果替换 headers = res_sub(headers,headers_data) if len(cookies_para): cookies_data = pre_res['body'][cookies_para[0]] # 结果替换 cookies= res_sub(cookies,cookies_data) return headers,cookies if __name__ == '__main__': report_path = get_report_path()+os.sep+"result" report_html= get_report_path()+os.sep+"html" pytest.main(['-s','test_excel_case.py','--alluredir',report_path]) allure_report(report_path ,report_html) #动态关联 # 1、验证前置条件 # 2、找到执行用例 # 3、发送请求,获取前置用例结果 # 发送获取前置测试用例,用例结果 # 数据初始化,get/post,重构 # 4、替换headers变量 # 5、请求发送
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)