接口自动化——har 生成用例

2023-11-10 06:50

本文主要是介绍接口自动化——har 生成用例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里写目录标题

  • 一、目标
  • 二、应用场景
  • 三、Har 简介
  • 四、实现思路
  • 五、模板技术
  • 六、模版技术-环境安装(Python)
  • 七、har 生成用例实现思路
    • 1、python模板
      • 模板文件
      • 生成的测试文件
    • 2、java模板
      • 模板文件
      • 生成的测试文件
    • 3、httprunner模板
      • 模板文件
      • 生成的测试文件

一、目标

掌握 Har 转换成脚本的能力。

二、应用场景

通过 Har 格式的接口数据,转换为接口自动化测试脚本:
提升脚本的编写效率
降低脚本的出BUG的几率

三、Har 简介

Har格式是指HTTP归档格式(HTTP Archive Format)。
用于记录HTTP会话信息的文件格式。
多个浏览器都可以生成 Har 格式的数据。

四、实现思路

在这里插入图片描述

五、模板技术

Mustache是一种轻量级的模板语言。
需要定义模板,然后可以将数据填充到设定好的位置。
官网地址:https://mustache.github.io/

模版
Hello {{name}}!
填充name的位置
Hello KOBE!

六、模版技术-环境安装(Python)

环境安装

pip install chevron

七、har 生成用例实现思路

读取Har数据。
提前准备测试用例模版。
将读取的Har数据写入到模板中。

代码实现如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/5/10 11:15
# @Author  : 杜兰特
# @File    : generate_case.py
import json
import chevronclass GenerateCase:def load_har(self,har_file):"""从har格式的文件中提取想要的数据信息:return:"""with open(har_file) as f:har_file_data=json.load(f)#print(har_file_data)print(har_file_data["log"]["entries"][0]["request"])return har_file_data["log"]["entries"][0]["request"]def generate_case_by_har(self,orgin_template,testcase_filename,har_file):"""生成对应的测试用例1、读取原本的模板文件2、替换模板文件中的数据信息3、生成新的测试用例文件:return:"""har_data=self.load_har(har_file)with open(orgin_template,encoding="utf-8") as f:res=chevron.render(f.read(),har_data)with open(testcase_filename,"w",encoding="utf-8") as f:f.write(res)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/5/10 11:18
# @Author  : 杜兰特
# @File    : test_generate_case.pyfrom L5.har_to_case.generate_case import GenerateCasegenerate_case=GenerateCase()def test_load_har():generate_case.load_har("./template/httpbin.ceshiren.com.har")def test_generate_case_by_har():generate_case.generate_case_by_har("./template/python_template","test_req.py","./template/httpbin.ceshiren.com.har")def test_generate_httprunner_case_by_har():generate_case.generate_case_by_har("./template/httprunner_template","test_req.yaml","./template/httpbin.ceshiren.com.har")def test_generate_java_case_by_har():generate_case.generate_case_by_har("./template/java_template","test_req.java","./template/httpbin.ceshiren.com.har")

1、python模板

模板文件

# python 接口测试用例
import requests
def test_request():r = requests.get(url="{{url}}",method="{{method}}",headers="{{headers}}")

生成的测试文件

# python 接口测试用例
import requests
def test_request():r = requests.get(url="https://httpbin.ceshiren.com/",method="GET",headers="[{'name': ':authority', 'value': 'httpbin.ceshiren.com'}, {'name': ':method', 'value': 'GET'}, {'name': ':path', 'value': '/'}, {'name': ':scheme', 'value': 'https'}, {'name': 'accept', 'value': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'}, {'name': 'accept-encoding', 'value': 'gzip, deflate, br'}, {'name': 'accept-language', 'value': 'zh-CN,zh;q=0.9'}, {'name': 'cache-control', 'value': 'max-age=0'}, {'name': 'cookie', 'value': '_ga=GA1.2.176134381.1632832298; sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22186920d31dd1499-00e17cfac93039-26031951-1821369-186920d31de12dd%22%7D; Hm_lvt_214f62eef822bde113f63fedcab70931=1681872517,1682320279,1683459070; Hm_lpvt_214f62eef822bde113f63fedcab70931=1683459388'}, {'name': 'sec-ch-ua', 'value': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"'}, {'name': 'sec-ch-ua-mobile', 'value': '?0'}, {'name': 'sec-ch-ua-platform', 'value': '"Windows"'}, {'name': 'sec-fetch-dest', 'value': 'document'}, {'name': 'sec-fetch-mode', 'value': 'navigate'}, {'name': 'sec-fetch-site', 'value': 'none'}, {'name': 'sec-fetch-user', 'value': '?1'}, {'name': 'upgrade-insecure-requests', 'value': '1'}, {'name': 'user-agent', 'value': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}]")

2、java模板

模板文件

// java 接口测试用例
package com.ceshiren.har;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class HttpbinTest {@Testvoid httpbinReq() {given()// 可以设置测试预设.when()// 发起 GET 请求.get("{{url}}").then()// 解析结果.log().all()  // 打印完整响应信息.statusCode(200);  // 响应断言}
}

生成的测试文件

// java 接口测试用例
package com.ceshiren.har;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class HttpbinTest {@Testvoid httpbinReq() {given()// 可以设置测试预设.when()// 发起 GET 请求.get("https://httpbin.ceshiren.com/").then()// 解析结果.log().all()  // 打印完整响应信息.statusCode(200);  // 响应断言}
}

3、httprunner模板

模板文件

# httprunner 的用例模版
config:name: basic test with httpbin
teststeps:
-name: httprunner的模板request:url: {{url}}method: GETvalidate_script:- "assert status_code == 200"

生成的测试文件

# httprunner 的用例模版
config:name: basic test with httpbin
teststeps:
-name: httprunner的模板request:url: https://httpbin.ceshiren.com/method: GETvalidate_script:- "assert status_code == 200"

这篇关于接口自动化——har 生成用例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/381037

相关文章

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

Java使用POI-TL和JFreeChart动态生成Word报告

《Java使用POI-TL和JFreeChart动态生成Word报告》本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码... 目录前言一、需求背景二、方案分析三、 POI-TL + JFreeChart 实现3.1 Maven

10个Python自动化办公的脚本分享

《10个Python自动化办公的脚本分享》在日常办公中,我们常常会被繁琐、重复的任务占据大量时间,本文为大家分享了10个实用的Python自动化办公案例及源码,希望对大家有所帮助... 目录1. 批量处理 Excel 文件2. 自动发送邮件3. 批量重命名文件4. 数据清洗5. 生成 PPT6. 自动化测试

10个Python Excel自动化脚本分享

《10个PythonExcel自动化脚本分享》在数据处理和分析的过程中,Excel文件是我们日常工作中常见的格式,本文将分享10个实用的Excel自动化脚本,希望可以帮助大家更轻松地掌握这些技能... 目录1. Excel单元格批量填充2. 设置行高与列宽3. 根据条件删除行4. 创建新的Excel工作表5

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创