SoFunction
Updated on 2024-11-15

Implementing an intelligent use case generation tool based on python

role of tools

Generate generic test points based on input function points

Implementation steps

The tool is realized in 2 main steps:

Request to call Gpt, will return the response to save the results as a .md file

2. python implementation of the .md file into a .xmind file

3. Write a simple front-end page to call the above step interface

Detailed Code

1. Call gpt request to generate md file

import os
import requests
import json
"""Test Data Path Management"""
SCRIPTS_DIR = (((__file__)))
DATAS_DIR = (SCRIPTS_DIR, "datas")
Testcase_md= (DATAS_DIR,'')
"""Sample code"""
def sendGpt_getmd(content):
    # Set the token in the request header
    headers = {
        "Content-Type": "application/json"
    }
    # payload= set the parameters and data in the request body, just pass the parameters according to the format of the request parameters
    payload = {"Parameters": "Parameter values"}
    # Send POST request
    response = ("https://xxxxx", headers=headers,
                             data=(payload))
    # Parse the response
    if response.status_code == 200:
        result = 
        print(result)
        with open(Testcase_md, "w", encoding="utf-8") as f:
            (result)
    else:
        print("Request failed!")
if __name__ == '__main__':
  # Example usage: keydes is the title of the function, casecontentTemp is the template sent to gpt, in the use of the keydes can be set as a variable, by the front-end to pass the parameter. In addition, you can modify the content of casecontentTemp according to your needs.
  # Markdown format with only first level headings, second level headings, unordered list format: this format cannot be modified as it needs to be converted to xmind file in Markdown format subsequently
  keydes='Upload Attachment'
  casecontentTemp="Send it to me in Markdown form containing only primary headings, secondary headings, and unordered list format. Please write the primary heading as '"+keydes+"'s test cases containing functional tests (forward/reverse/anomaly scenarios), performance tests, security tests, compatibility tests"
  sendGpt_getmd(casecontentTemp)

2. Convert md file to xmind file

With step 1, the md file is generated and the following code is to convert the md file into an xmind file

import markdown
from bs4 import BeautifulSoup
import xmind
def md_to_xmind(md_file, xmind_file):
    # Read the MD file
    with open(md_file, 'r', encoding='utf-8') as f:
        md = ()
    # Parsing MD files
    html = (md)
    # Create XMind files
    workbook = (xmind_file)
    # Get the root node
    root_topic = ().getRootTopic()
    # Recursively add nodes
    def add_topic(parent_topic, node):
        # Add node
        topic = parent_topic.addSubTopic()
        title = ('title', '')
        (title)
        # Add text
        if 'html' in node:
            (node['html'])
        # Recursively add child nodes
        if 'children' in node:
            for child in node['children']:
                add_topic(topic, child)
    # Parsing HTML and adding nodes
    soup = BeautifulSoup(html, '')
    rootmap_node = {'children': []}
    root_node = None
    current_node = None
    for tag in :
        if  == 'h1':
            # Create the root node
            root_node = {'title': , 'children': []}
            current_node = root_node
        elif  == 'h2':
            new_node = {'title': , 'children': []}
            root_node['children'].append(new_node)
            current_node = new_node
        elif  == 'p':
            current_node['html'] = str(tag)
        elif  == 'ul':
            for li in :
                text = ()
                if len(text) > 0:
                    li_node = {'title': text, 'children': []}
                    current_node['children'].append(li_node)
        elif  == 'ol':
            for li in :
                text = ()
                if len(text) > 0:
                    li_node = {'title': text, 'children': []}
                    current_node['children'].append(li_node)
    # Add node
    for node in root_node['children']:
        add_topic(root_topic, node)
    # Change the name of the root node
    root_topic.setTitle(root_node['title'])
    # Save XMind files
    (workbook, xmind_file)
if __name__ == '__main__':
  # Example Usage Convert an .md file to an xmind file by calling the md_to_xmind method for the file generated in step 1.
  md_to_xmind('', '')
 

3. Combine steps 1 and 2 and run.

import os
from  import md_to_xmind
from  import sendGpt_getmd
"""Test Data Path Management"""
SCRIPTS_DIR = (((__file__)))
DATAS_DIR = (SCRIPTS_DIR, "datas")
Testcase_md= (DATAS_DIR,'')
Testcase_xmind= (DATAS_DIR,'')
def oneTocase(keydes):
   casecontentTemp="Send it to me in Markdown form containing only primary headings, secondary headings, and unordered list format. Please write the primary heading as '"+keydes+"'s test cases containing functional tests (forward/reverse/anomaly scenarios), performance tests, security tests, compatibility tests"
    sendGpt_getmd(casecontentTemp)
    md_to_xmind(Testcase_md, Testcase_xmind)
if __name__ == '__main__':
  # Example usage
  keydes='Upload Attachment'
  oneTocase(keydes)

Post-run results:

Generate corresponding files

Open it to view the following

Post-integration test tool conceptualization

1. Expose the rest interface in step 3 of the detailed code for front-end calls

2. Front-end pages can be provided out of the formation General tools

to this article on the implementation of intelligent use case generation tool based on python to this article, more relevant python intelligent generation tool content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!