Update backend.py
Browse files- backend.py +76 -53
backend.py
CHANGED
|
@@ -3,7 +3,9 @@ import io
|
|
| 3 |
import requests
|
| 4 |
import html # For escaping HTML characters
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
-
import pandas as pd
|
|
|
|
|
|
|
| 7 |
from openai import OpenAI
|
| 8 |
|
| 9 |
# Initialize OpenAI API with Nvidia's Llama model
|
|
@@ -24,26 +26,19 @@ def clean_test_case_output(text):
|
|
| 24 |
def generate_testcases(user_story):
|
| 25 |
"""
|
| 26 |
Generates advanced QA test cases based on a provided user story by interacting
|
| 27 |
-
with Nvidia's llama model API.
|
| 28 |
-
and the output is processed for better quality.
|
| 29 |
|
| 30 |
:param user_story: A string representing the user story for which to generate test cases.
|
| 31 |
-
:return: A list of
|
| 32 |
"""
|
| 33 |
-
|
| 34 |
-
# Few-shot learning examples to guide the model
|
| 35 |
few_shot_examples = """
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"Understand the story thoroughly"
|
| 39 |
-
"If it's a DropBury or ODAC Portal User Story, then we perform testing in ODAC Portal"
|
| 40 |
"""
|
| 41 |
|
| 42 |
-
# Combine the few-shot examples with the user story for the model to process
|
| 43 |
prompt = few_shot_examples + f"\nUser Story: {user_story}\n"
|
| 44 |
|
| 45 |
try:
|
| 46 |
-
# Call the Nvidia llama API with the refined prompt
|
| 47 |
completion = client.chat.completions.create(
|
| 48 |
model="meta/llama-3.1-405b-instruct",
|
| 49 |
messages=[
|
|
@@ -55,37 +50,65 @@ def generate_testcases(user_story):
|
|
| 55 |
stream=True
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# Initialize an empty string to accumulate the response
|
| 59 |
test_cases_text = ""
|
| 60 |
|
| 61 |
-
# Accumulate the response from the streaming chunks
|
| 62 |
for chunk in completion:
|
| 63 |
if chunk.choices[0].delta.content is not None:
|
| 64 |
test_cases_text += chunk.choices[0].delta.content
|
| 65 |
|
| 66 |
-
# Ensure the entire response is captured before cleaning
|
| 67 |
if test_cases_text.strip() == "":
|
| 68 |
return [{"test_case": "No test cases generated or output was empty."}]
|
| 69 |
|
| 70 |
-
# Clean the output by unescaping HTML entities and replacing <br> tags
|
| 71 |
test_cases_text = clean_test_case_output(test_cases_text)
|
| 72 |
|
| 73 |
try:
|
| 74 |
-
# Try to parse the output as JSON, assuming the model returns structured test cases
|
| 75 |
test_cases = json.loads(test_cases_text)
|
| 76 |
if isinstance(test_cases, list):
|
| 77 |
-
return test_cases
|
| 78 |
else:
|
| 79 |
-
return [{"test_case": test_cases_text}]
|
| 80 |
-
|
| 81 |
except json.JSONDecodeError:
|
| 82 |
-
#
|
| 83 |
-
return
|
| 84 |
|
| 85 |
except requests.exceptions.RequestException as e:
|
| 86 |
print(f"API request failed: {str(e)}")
|
| 87 |
return []
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def export_test_cases(test_cases):
|
| 90 |
"""
|
| 91 |
Exports the test cases to an Excel file with specific columns:
|
|
@@ -94,39 +117,21 @@ def export_test_cases(test_cases):
|
|
| 94 |
- Steps
|
| 95 |
- Expected Result
|
| 96 |
|
| 97 |
-
:param test_cases: A list of test case dictionaries
|
| 98 |
:return: Bytes of the Excel file.
|
| 99 |
"""
|
| 100 |
if not test_cases:
|
| 101 |
return "No test cases to export."
|
| 102 |
|
| 103 |
-
# Define the structure of the Excel file
|
| 104 |
formatted_test_cases = []
|
| 105 |
|
| 106 |
for case in test_cases:
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
preconditions = ""
|
| 114 |
-
steps = ""
|
| 115 |
-
expected_result = ""
|
| 116 |
-
|
| 117 |
-
for line in lines:
|
| 118 |
-
if "Preconditions" in line:
|
| 119 |
-
preconditions = line.replace("Preconditions:", "").strip()
|
| 120 |
-
elif "Steps" in line:
|
| 121 |
-
steps = line.replace("Steps:", "").strip()
|
| 122 |
-
elif "Expected Result" in line:
|
| 123 |
-
expected_result = line.replace("Expected Result:", "").strip()
|
| 124 |
-
else:
|
| 125 |
-
# Default to putting the first part as the "Test Case"
|
| 126 |
-
if not test_case:
|
| 127 |
-
test_case = line.strip()
|
| 128 |
-
|
| 129 |
-
# Append to formatted test cases list
|
| 130 |
formatted_test_cases.append({
|
| 131 |
'Test Case': test_case,
|
| 132 |
'Preconditions': preconditions,
|
|
@@ -134,12 +139,30 @@ def export_test_cases(test_cases):
|
|
| 134 |
'Expected Result': expected_result
|
| 135 |
})
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return output.getvalue()
|
|
|
|
| 3 |
import requests
|
| 4 |
import html # For escaping HTML characters
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from openpyxl import Workbook
|
| 8 |
+
from openpyxl.styles import Alignment, Font
|
| 9 |
from openai import OpenAI
|
| 10 |
|
| 11 |
# Initialize OpenAI API with Nvidia's Llama model
|
|
|
|
| 26 |
def generate_testcases(user_story):
|
| 27 |
"""
|
| 28 |
Generates advanced QA test cases based on a provided user story by interacting
|
| 29 |
+
with Nvidia's llama model API.
|
|
|
|
| 30 |
|
| 31 |
:param user_story: A string representing the user story for which to generate test cases.
|
| 32 |
+
:return: A list of dictionaries with test case information.
|
| 33 |
"""
|
|
|
|
|
|
|
| 34 |
few_shot_examples = """
|
| 35 |
+
"Generate as many test cases as possible. Minimum 6, but can be more."
|
| 36 |
+
"Structure each test case with Test Case, Preconditions, Steps, and Expected Result."
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
|
|
|
|
| 39 |
prompt = few_shot_examples + f"\nUser Story: {user_story}\n"
|
| 40 |
|
| 41 |
try:
|
|
|
|
| 42 |
completion = client.chat.completions.create(
|
| 43 |
model="meta/llama-3.1-405b-instruct",
|
| 44 |
messages=[
|
|
|
|
| 50 |
stream=True
|
| 51 |
)
|
| 52 |
|
|
|
|
| 53 |
test_cases_text = ""
|
| 54 |
|
|
|
|
| 55 |
for chunk in completion:
|
| 56 |
if chunk.choices[0].delta.content is not None:
|
| 57 |
test_cases_text += chunk.choices[0].delta.content
|
| 58 |
|
|
|
|
| 59 |
if test_cases_text.strip() == "":
|
| 60 |
return [{"test_case": "No test cases generated or output was empty."}]
|
| 61 |
|
|
|
|
| 62 |
test_cases_text = clean_test_case_output(test_cases_text)
|
| 63 |
|
| 64 |
try:
|
|
|
|
| 65 |
test_cases = json.loads(test_cases_text)
|
| 66 |
if isinstance(test_cases, list):
|
| 67 |
+
return test_cases
|
| 68 |
else:
|
| 69 |
+
return [{"test_case": test_cases_text}]
|
|
|
|
| 70 |
except json.JSONDecodeError:
|
| 71 |
+
# If JSON decoding fails, attempt to parse manually
|
| 72 |
+
return parse_test_cases(test_cases_text)
|
| 73 |
|
| 74 |
except requests.exceptions.RequestException as e:
|
| 75 |
print(f"API request failed: {str(e)}")
|
| 76 |
return []
|
| 77 |
|
| 78 |
+
def parse_test_cases(raw_text):
|
| 79 |
+
"""
|
| 80 |
+
Parse raw text output into structured test cases.
|
| 81 |
+
|
| 82 |
+
:param raw_text: Raw text returned from the model.
|
| 83 |
+
:return: List of dictionaries with structured test cases.
|
| 84 |
+
"""
|
| 85 |
+
test_cases = []
|
| 86 |
+
case = {
|
| 87 |
+
"Test Case": "",
|
| 88 |
+
"Preconditions": "N/A",
|
| 89 |
+
"Steps": "N/A",
|
| 90 |
+
"Expected Result": "N/A"
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
lines = raw_text.split("\n")
|
| 94 |
+
for line in lines:
|
| 95 |
+
if line.startswith("Test Case"):
|
| 96 |
+
if case["Test Case"]:
|
| 97 |
+
test_cases.append(case) # Save the previous test case
|
| 98 |
+
case = {"Test Case": "", "Preconditions": "N/A", "Steps": "N/A", "Expected Result": "N/A"}
|
| 99 |
+
case["Test Case"] = line.replace("Test Case:", "").strip()
|
| 100 |
+
elif "Preconditions" in line:
|
| 101 |
+
case["Preconditions"] = line.replace("Preconditions:", "").strip() or "N/A"
|
| 102 |
+
elif "Steps" in line:
|
| 103 |
+
case["Steps"] = line.replace("Steps:", "").strip() or "N/A"
|
| 104 |
+
elif "Expected Result" in line:
|
| 105 |
+
case["Expected Result"] = line.replace("Expected Result:", "").strip() or "N/A"
|
| 106 |
+
|
| 107 |
+
if case["Test Case"]: # Add the last case
|
| 108 |
+
test_cases.append(case)
|
| 109 |
+
|
| 110 |
+
return test_cases
|
| 111 |
+
|
| 112 |
def export_test_cases(test_cases):
|
| 113 |
"""
|
| 114 |
Exports the test cases to an Excel file with specific columns:
|
|
|
|
| 117 |
- Steps
|
| 118 |
- Expected Result
|
| 119 |
|
| 120 |
+
:param test_cases: A list of test case dictionaries.
|
| 121 |
:return: Bytes of the Excel file.
|
| 122 |
"""
|
| 123 |
if not test_cases:
|
| 124 |
return "No test cases to export."
|
| 125 |
|
|
|
|
| 126 |
formatted_test_cases = []
|
| 127 |
|
| 128 |
for case in test_cases:
|
| 129 |
+
# Ensure each field has a default value if missing
|
| 130 |
+
test_case = case.get('Test Case', 'N/A')
|
| 131 |
+
preconditions = case.get('Preconditions', 'N/A')
|
| 132 |
+
steps = case.get('Steps', 'N/A')
|
| 133 |
+
expected_result = case.get('Expected Result', 'N/A')
|
| 134 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
formatted_test_cases.append({
|
| 136 |
'Test Case': test_case,
|
| 137 |
'Preconditions': preconditions,
|
|
|
|
| 139 |
'Expected Result': expected_result
|
| 140 |
})
|
| 141 |
|
| 142 |
+
wb = Workbook()
|
| 143 |
+
ws = wb.active
|
| 144 |
+
ws.title = "Test Cases"
|
| 145 |
+
|
| 146 |
+
# Add headers with formatting
|
| 147 |
+
headers = ["Test Case", "Preconditions", "Steps", "Expected Result"]
|
| 148 |
+
ws.append(headers)
|
| 149 |
|
| 150 |
+
for cell in ws[1]:
|
| 151 |
+
cell.font = Font(bold=True)
|
| 152 |
+
cell.alignment = Alignment(horizontal="center", vertical="center")
|
| 153 |
+
|
| 154 |
+
# Add the test case data
|
| 155 |
+
for case in formatted_test_cases:
|
| 156 |
+
ws.append([case["Test Case"], case["Preconditions"], case["Steps"], case["Expected Result"]])
|
| 157 |
|
| 158 |
+
# Adjust column widths for neatness
|
| 159 |
+
ws.column_dimensions['A'].width = 50 # Test Case
|
| 160 |
+
ws.column_dimensions['B'].width = 30 # Preconditions
|
| 161 |
+
ws.column_dimensions['C'].width = 50 # Steps
|
| 162 |
+
ws.column_dimensions['D'].width = 50 # Expected Result
|
| 163 |
+
|
| 164 |
+
output = io.BytesIO()
|
| 165 |
+
wb.save(output)
|
| 166 |
+
output.seek(0)
|
| 167 |
+
|
| 168 |
return output.getvalue()
|