ArvindSelvaraj commited on
Commit
9255728
1 Parent(s): 0a784fa

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +30 -45
backend.py CHANGED
@@ -64,64 +64,49 @@ def generate_testcases(user_story):
64
  stream=True
65
  )
66
 
 
67
  test_cases_text = ""
68
 
 
69
  for chunk in completion:
70
  if chunk.choices[0].delta.content is not None:
71
  test_cases_text += chunk.choices[0].delta.content
72
 
 
 
 
 
73
  if test_cases_text.strip() == "":
74
- return [{"test_case": "No test cases generated or output was empty."}]
75
 
 
76
  test_cases_text = clean_test_case_output(test_cases_text)
77
 
78
- try:
79
- test_cases = json.loads(test_cases_text)
80
- if isinstance(test_cases, list):
81
- return test_cases
82
- else:
83
- return [{"test_case": test_cases_text}]
84
- except json.JSONDecodeError:
85
- # If JSON decoding fails, attempt to parse manually
86
- return parse_test_cases(test_cases_text)
87
 
88
- except requests.exceptions.RequestException as e:
89
- print(f"API request failed: {str(e)}")
90
- return []
 
 
 
 
 
 
 
 
 
 
91
 
92
- def parse_test_cases(raw_text):
93
- """
94
- Parse raw text output into structured test cases.
95
-
96
- :param raw_text: Raw text returned from the model.
97
- :return: List of dictionaries with structured test cases.
98
- """
99
- test_cases = []
100
- case = {
101
- "Test Case": "",
102
- "Preconditions": "N/A",
103
- "Steps": "N/A",
104
- "Expected Result": "N/A"
105
- }
106
-
107
- lines = raw_text.split("\n")
108
- for line in lines:
109
- if line.startswith("Test Case"):
110
- if case["Test Case"]:
111
- test_cases.append(case) # Save the previous test case
112
- case = {"Test Case": "", "Preconditions": "N/A", "Steps": "N/A", "Expected Result": "N/A"}
113
- case["Test Case"] = line.replace("Test Case:", "").strip()
114
- elif "Preconditions" in line:
115
- case["Preconditions"] = line.replace("Preconditions:", "").strip() or "N/A"
116
- elif "Steps" in line:
117
- case["Steps"] = line.replace("Steps:", "").strip() or "N/A"
118
- elif "Expected Result" in line:
119
- case["Expected Result"] = line.replace("Expected Result:", "").strip() or "N/A"
120
-
121
- if case["Test Case"]: # Add the last case
122
- test_cases.append(case)
123
 
124
- return test_cases
 
 
125
 
126
  def export_test_cases(test_cases):
127
  """
 
64
  stream=True
65
  )
66
 
67
+ # Initialize an empty string to accumulate the response
68
  test_cases_text = ""
69
 
70
+ # Accumulate the response from the streaming chunks
71
  for chunk in completion:
72
  if chunk.choices[0].delta.content is not None:
73
  test_cases_text += chunk.choices[0].delta.content
74
 
75
+ # Print raw response for debugging
76
+ print("Raw response from model:", test_cases_text)
77
+
78
+ # Ensure the entire response is captured before cleaning
79
  if test_cases_text.strip() == "":
80
+ return [{"Test Case": "No test cases generated or output was empty."}]
81
 
82
+ # Clean the output by unescaping HTML entities and replacing <br> tags
83
  test_cases_text = clean_test_case_output(test_cases_text)
84
 
85
+ # Print cleaned response for debugging
86
+ print("Cleaned response:", test_cases_text)
 
 
 
 
 
 
 
87
 
88
+ # Split the output into individual test cases by detecting patterns
89
+ test_case_blocks = test_cases_text.split('\n\n')
90
+ test_cases = []
91
+ for block in test_case_blocks:
92
+ lines = block.split('\n')
93
+ if len(lines) >= 4:
94
+ test_case = {
95
+ 'Test Case': lines[0].replace('Test Case ', '').strip(),
96
+ 'Preconditions': lines[1].replace('Preconditions: ', '').strip(),
97
+ 'Steps': lines[2].replace('Steps: ', '').strip(),
98
+ 'Expected Result': lines[3].replace('Expected Result: ', '').strip(),
99
+ }
100
+ test_cases.append(test_case)
101
 
102
+ if not test_cases:
103
+ return [{"Test Case": "No test cases generated or output was empty."}]
104
+
105
+ return test_cases
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
+ except requests.exceptions.RequestException as e:
108
+ print(f"API request failed: {str(e)}")
109
+ return [{"Test Case": "API request failed."}]
110
 
111
  def export_test_cases(test_cases):
112
  """