Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
import sqlite3
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
# Load AI model for test case generation
|
| 9 |
+
test_case_generator = pipeline("text-generation", model="microsoft/CodeGPT-small")
|
| 10 |
+
|
| 11 |
+
def fetch_swagger_data(swagger_url):
|
| 12 |
+
try:
|
| 13 |
+
response = requests.get(swagger_url)
|
| 14 |
+
return response.json()
|
| 15 |
+
except Exception as e:
|
| 16 |
+
st.error(f"Error fetching Swagger data: {e}")
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
def generate_bdd_test_cases(swagger_data):
|
| 20 |
+
test_cases = {}
|
| 21 |
+
for path, methods in swagger_data.get("paths", {}).items():
|
| 22 |
+
for method, details in methods.items():
|
| 23 |
+
prompt = f"Generate BDD test case for {method.upper()} {path} with {details.get('parameters', [])}"
|
| 24 |
+
ai_response = test_case_generator(prompt, max_length=200, num_return_sequences=1)
|
| 25 |
+
test_cases[f"{method.upper()} {path}"] = ai_response[0]['generated_text']
|
| 26 |
+
return test_cases
|
| 27 |
+
|
| 28 |
+
def execute_test_script(test_case):
|
| 29 |
+
# Simulate API call execution
|
| 30 |
+
st.write(f"Executing test: {test_case}")
|
| 31 |
+
return {"status": "Passed", "response_time": "200ms"}
|
| 32 |
+
|
| 33 |
+
def store_test_results(test_case, result):
|
| 34 |
+
conn = sqlite3.connect("test_results.db")
|
| 35 |
+
cursor = conn.cursor()
|
| 36 |
+
cursor.execute("""
|
| 37 |
+
CREATE TABLE IF NOT EXISTS results (test_case TEXT, status TEXT, response_time TEXT)
|
| 38 |
+
""")
|
| 39 |
+
cursor.execute("INSERT INTO results (test_case, status, response_time) VALUES (?, ?, ?)",
|
| 40 |
+
(test_case, result["status"], result["response_time"]))
|
| 41 |
+
conn.commit()
|
| 42 |
+
conn.close()
|
| 43 |
+
|
| 44 |
+
def show_past_results():
|
| 45 |
+
conn = sqlite3.connect("test_results.db")
|
| 46 |
+
cursor = conn.cursor()
|
| 47 |
+
cursor.execute("SELECT * FROM results")
|
| 48 |
+
results = cursor.fetchall()
|
| 49 |
+
conn.close()
|
| 50 |
+
return results
|
| 51 |
+
|
| 52 |
+
# Streamlit UI
|
| 53 |
+
st.title("AI-Powered API Test Case Generator")
|
| 54 |
+
swagger_url = st.text_input("Enter Swagger URL")
|
| 55 |
+
if st.button("Generate Test Cases"):
|
| 56 |
+
swagger_data = fetch_swagger_data(swagger_url)
|
| 57 |
+
if swagger_data:
|
| 58 |
+
test_cases = generate_bdd_test_cases(swagger_data)
|
| 59 |
+
st.session_state["test_cases"] = test_cases
|
| 60 |
+
st.success("Test Cases Generated!")
|
| 61 |
+
st.json(test_cases)
|
| 62 |
+
|
| 63 |
+
if "test_cases" in st.session_state:
|
| 64 |
+
st.subheader("Execute Test Cases")
|
| 65 |
+
for api, test_case in st.session_state["test_cases"].items():
|
| 66 |
+
if st.button(f"Run {api}"):
|
| 67 |
+
result = execute_test_script(test_case)
|
| 68 |
+
store_test_results(api, result)
|
| 69 |
+
st.success(f"{api} - {result['status']}")
|
| 70 |
+
|
| 71 |
+
st.subheader("Past Execution Results")
|
| 72 |
+
past_results = show_past_results()
|
| 73 |
+
st.table(past_results)
|