python调用GPT实现:智能用例生成工具

工具作用:

根据输入的功能点,生成通用测试点

实现步骤

工具实现主要分2个步骤:

1.https请求调用Gpt,将返回响应结果保存为.md文件

2.用python实现 将 .md文件转换成.xmind文件

3.写个简单的前端页面,调用上述步骤接口

详细代码

1.调用gpt请求生成 md文件

import os
import requests
import json

"""测试数据路径管理"""
SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATAS_DIR = os.path.join(SCRIPTS_DIR, "datas")
Testcase_md= os.path.join(DATAS_DIR,'testcase.md')

"""示例代码"""

def sendGpt_getmd(content):
    # 设置请求头中的token

    headers = {
        "Content-Type": "application/json"
    }

    # payload= 设置请求主体中的参数和数据,根据请求的参数格式传参即可
    payload = {"参数": "参数值"}

    # 发送POST请求
    response = requests.post("https://xxxxx", headers=headers,
                             data=json.dumps(payload))

    # 解析响应结果
    if response.status_code == 200:
        result = response.text
        print(result)
        with open(Testcase_md, "w", encoding="utf-8") as f:
            f.write(result)
    else:
        print("请求失败!")

if __name__ == '__main__':
  # 示例用法:keydes为功能标题,casecontentTemp为发送给gpt的模板话术,在使用时可以将keydes设置为变量,由前端传参。另外可以根据需要修改casecontentTemp内容
  # 仅包含一级标题,二级标题,无序列表格式的Markdown形式:该格式不可修改,因为后续需要以Markdown格式转换成xmind文件


  keydes='上传附件'
  casecontentTemp="仅包含一级标题,二级标题,无序列表格式的Markdown形式发送给我。请写出 一级标题为'"+keydes+"'的测试用例,包含功能测试(正向/逆向/异常场景),性能测试,安全测试,兼容测试"
  sendGpt_getmd(casecontentTemp)

2.将md文件转换成xmind文件

通过步骤1,生成了md文件,以下代码是将md文件转换成xmind文件

import markdown
from bs4 import BeautifulSoup
import xmind

def md_to_xmind(md_file, xmind_file):
    # 读取MD文件
    with open(md_file, 'r', encoding='utf-8') as f:
        md = f.read()

    # 解析MD文件
    html = markdown.markdown(md)

    # 创建XMind文件
    workbook = xmind.load(xmind_file)

    # 获取根节点
    root_topic = workbook.getPrimarySheet().getRootTopic()

    # 递归添加节点
    def add_topic(parent_topic, node):
        # 添加节点
        topic = parent_topic.addSubTopic()
        title = node.get('title', '')
        topic.setTitle(title)

        # 添加文本
        if 'html' in node:
            topic.setPlainNotes(node['html'])

        # 递归添加子节点
        if 'children' in node:
            for child in node['children']:
                add_topic(topic, child)

    # 解析HTML,并添加节点
    soup = BeautifulSoup(html, 'html.parser')
    rootmap_node = {'children': []}
    root_node = None
    current_node = None
    for tag in soup.children:
        if tag.name == 'h1':
            # 创建根节点
            root_node = {'title': tag.string, 'children': []}
            current_node = root_node
        elif tag.name == 'h2':
            new_node = {'title': tag.string, 'children': []}
            root_node['children'].append(new_node)
            current_node = new_node
        elif tag.name == 'p':
            current_node['html'] = str(tag)
        elif tag.name == 'ul':
            for li in tag.children:
                text = li.text.strip()
                if len(text) > 0:
                    li_node = {'title': text, 'children': []}
                    current_node['children'].append(li_node)
        elif tag.name == 'ol':
            for li in tag.children:
                text = li.text.strip()
                if len(text) > 0:
                    li_node = {'title': text, 'children': []}
                    current_node['children'].append(li_node)
    # 添加节点
    for node in root_node['children']:
        add_topic(root_topic, node)

    # 修改根节点的名称
    root_topic.setTitle(root_node['title'])

    # 保存XMind文件
    xmind.save(workbook, xmind_file)


if __name__ == '__main__':
  # 示例用法 example.md为步骤1生成的文件,通过md_to_xmind方法调用将.md文件转换成xmind文件
  
  md_to_xmind('example.md', 'example.xmind')

3.合并步骤1,2后运行

import os
from scripts.datas.mdtoxmind import md_to_xmind
from scripts.datas.sendGpt import sendGpt_getmd

"""测试数据路径管理"""
SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATAS_DIR = os.path.join(SCRIPTS_DIR, "datas")
Testcase_md= os.path.join(DATAS_DIR,'testcase.md')
Testcase_xmind= os.path.join(DATAS_DIR,'testcase.xmind')


def oneTocase(keydes):
   casecontentTemp="仅包含一级标题,二级标题,无序列表格式的Markdown形式发送给我。请写出 一级标题为'"+keydes+"'的测试用例,包含功能测试(正向/逆向/异常场景),性能测试,安全测试,兼容测试"
    sendGpt_getmd(casecontentTemp)
    md_to_xmind(Testcase_md, Testcase_xmind)

if __name__ == '__main__':
  # 示例用法
  keydes='上传附件'
  oneTocase(keydes)

运行后结果:

生成对应文件

打开后查看如下内容

后期集成测试工具构思:

1.将详细代码中步骤3暴露出rest接口,供前端调用

2.前端页面可提供出形成 通用工具

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海是倒过来的天~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值