datawithsuman commited on
Commit
46f624d
·
verified ·
1 Parent(s): 1f2d337

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -19
app.py CHANGED
@@ -272,9 +272,17 @@ class TestCaseGenerator:
272
  self.functions[method_name] = ''.join(method_body)
273
  method_body = []
274
 
 
 
 
 
 
 
 
 
275
  def generate_response(self, prompt):
276
  response = [
277
- ChatMessage(role="system", content="You are a sincere and helpful coding assistant"),
278
  ChatMessage(role="user", content=prompt),
279
  ]
280
  resp = MistralAI(self.model).chat(response)
@@ -286,26 +294,64 @@ class TestCaseGenerator:
286
  return
287
 
288
  snippet = self.functions[func]
289
- prompt = f"""Your task is to generate unit test cases with maximum coverage for this function : \n\n{snippet}\
290
- \n\n - Generate unique unit test cases. Include couple of edge cases as well.
291
- \n\n - Calculate the coverage percentage and display in the response.
292
- \n\n - All the test cases should have the mandatory assert statement.
293
- \n\n - Every test case should be defined as a method inside the class.
294
- \n\n - All the test cases should have textual description.
295
- \n\n - Politely refuse if the function is not suitable for generating test cases.
296
- \n\n - There should be no duplicate and incomplete test case.
297
- \n\n - Avoid generating repeated statements.
298
- \n\n - Recheck your response before generating.
299
- \n\n - Do not share the last Test Case.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  """
301
  resp = self.generate_response(prompt)
302
- post_prompt = f"""Except the last test case, display everything that is present in this end to end: \n\n{resp}\
303
- \n\n - Do not add anything extra. Just copy and paste everything except the last test case.
304
- \n\n - Do not mention the count of total number of test cases in the response.
305
- \n\n - Do not mention this sentence - "I have excluded the last test case as per your request"
306
- """
307
- post_resp = self.generate_response(post_prompt)
308
- return post_resp
 
309
 
310
  def run(self):
311
  self.setup_streamlit_ui()
 
272
  self.functions[method_name] = ''.join(method_body)
273
  method_body = []
274
 
275
+ # def generate_response(self, prompt):
276
+ # response = [
277
+ # ChatMessage(role="system", content="You are a sincere and helpful coding assistant"),
278
+ # ChatMessage(role="user", content=prompt),
279
+ # ]
280
+ # resp = MistralAI(self.model).chat(response)
281
+ # return resp
282
+
283
  def generate_response(self, prompt):
284
  response = [
285
+ ChatMessage(role="system", content="You are tasked with generating unit test cases, including descriptions and assert statements, for a given function. You will also calculate the test coverage percentage. Follow these instructions carefully:"),
286
  ChatMessage(role="user", content=prompt),
287
  ]
288
  resp = MistralAI(self.model).chat(response)
 
294
  return
295
 
296
  snippet = self.functions[func]
297
+ , file_extension = os.path.splitext(uploaded_file.name)
298
+ lang = file_extension[1:]
299
+
300
+ prompt = f"""1. You will be provided with the following inputs:
301
+ <function_code>
302
+ {snippet}
303
+ </function_code>
304
+
305
+ <language>{lang}</language>
306
+
307
+ 2. Analyze the provided function:
308
+ - Identify the function name, parameters, and return type
309
+ - Determine the main logic and branches in the function
310
+ - Note any potential edge cases or boundary conditions
311
+
312
+ 3. Generate unit test cases:
313
+ - Create at least 3-5 test cases that cover different scenarios
314
+ - Include normal cases, edge cases, and potential error conditions
315
+ - Ensure that the test cases collectively cover all branches and logic paths in the function
316
+
317
+ 4. For each test case, provide:
318
+ - A brief description of the test scenario
319
+ - Input values for the function parameters
320
+ - The expected output or behavior
321
+ - An assert statement in the appropriate syntax for the given programming language
322
+
323
+ 5. Calculate the test coverage percentage:
324
+ - Determine the number of code paths or branches in the function
325
+ - Count how many of these paths are covered by your test cases
326
+ - Calculate the percentage: (covered paths / total paths) * 100
327
+
328
+ 6. Present your output in the following format:
329
+ <unit_tests>
330
+ <test_case>
331
+ <description>Description of the test case</description>
332
+ <input>Input values for the function</input>
333
+ <expected_output>Expected output or behavior</expected_output>
334
+ <assert_statement>Assert statement in the appropriate language syntax</assert_statement>
335
+ </test_case>
336
+ <!-- Repeat for each test case -->
337
+ </unit_tests>
338
+
339
+ <test_coverage>
340
+ <percentage>Calculated test coverage percentage</percentage>
341
+ <explanation>Brief explanation of how the percentage was calculated</explanation>
342
+ </test_coverage>
343
+
344
+ Remember to adapt your assert statements and syntax to the specific programming language provided. If you're unsure about the exact syntax for a particular language, use a general pseudocode format that clearly conveys the assertion logic.
345
  """
346
  resp = self.generate_response(prompt)
347
+
348
+ # post_prompt = f"""Except the last test case, display everything that is present in this end to end: \n\n{resp}\
349
+ # \n\n - Do not add anything extra. Just copy and paste everything except the last test case.
350
+ # \n\n - Do not mention the count of total number of test cases in the response.
351
+ # \n\n - Do not mention this sentence - "I have excluded the last test case as per your request"
352
+ # """
353
+ # post_resp = self.generate_response(post_prompt)
354
+ return resp
355
 
356
  def run(self):
357
  self.setup_streamlit_ui()