Skip to content

Commit dbdb1ba

Browse files
authored
Merge pull request #53 from devchat-ai/improve-propose-tests
feat: maintain a balance between happy paths and edge cases
2 parents 6aaeac7 + d8866a5 commit dbdb1ba

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

unit_tests/main.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import click
66
from find_reference_tests import find_reference_tests
77
from i18n import TUILanguage, get_translation
8-
from model import FuncToTest, TokenBudgetExceededException, UserCancelledException
8+
from model import (
9+
FuncToTest,
10+
TokenBudgetExceededException,
11+
UserCancelledException,
12+
)
913
from propose_test import propose_test
1014
from tools.file_util import retrieve_file_content
1115
from write_tests import write_and_print_tests
@@ -39,7 +43,9 @@ def run(self):
3943

4044
self.step3_write_and_print_tests(cases, files)
4145

42-
def step1_propose_cases_and_reference_files(self) -> Tuple[List[str], List[str]]:
46+
def step1_propose_cases_and_reference_files(
47+
self,
48+
) -> Tuple[List[str], List[str]]:
4349
"""
4450
Propose test cases and reference files for a specified function.
4551
@@ -60,7 +66,9 @@ def step1_propose_cases_and_reference_files(self) -> Tuple[List[str], List[str]]
6066
)
6167

6268
ref_files = find_reference_tests(
63-
self.repo_root, self.func_to_test.func_name, self.func_to_test.file_path
69+
self.repo_root,
70+
self.func_to_test.func_name,
71+
self.func_to_test.file_path,
6472
)
6573

6674
if ref_files:
@@ -122,7 +130,7 @@ def step2_edit_cases_and_reference_files(
122130
try:
123131
retrieve_file_content(file_path=ref_file, root_path=self.repo_root)
124132
valid_files.append(ref_file)
125-
except Exception as e:
133+
except Exception:
126134
invalid_files.append(ref_file)
127135

128136
# Print summary

unit_tests/prompts.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
PROPOSE_TEST_PROMPT = """
66
You're an advanced AI test case generator.
7-
Given a user prompt and a target function, propose test cases for the function based on the prompt.
7+
Given a user prompt and a target function, propose detailed test cases for the function based on the prompt, categorizing each as either a 'happy path' or an 'edge case'.
88
99
The user prompt is as follows:
1010
@@ -16,15 +16,26 @@
1616
1717
{relevant_content}
1818
19-
Propose each test case with a one-line description of what behavior it tests.
19+
For each test case, provide a description that includes:
20+
- A brief explanation of the test's purpose
21+
- The specific conditions being tested.
22+
- The expected outcome the test is verifying.
23+
24+
Categorize each test case as:
25+
- 'happy path': Tests the function with typical inputs and standard conditions, ensuring it performs as expected in normal use.
26+
- 'edge case': Tests the function with atypical inputs or in unusual conditions, checking its robustness and error handling.
27+
2028
You don't have to write the test cases in code, just describe them in plain {chat_language}.
21-
Do not generate more than 6 test cases.
29+
30+
Aim to generate 3 'happy path' test cases and 3 'edge case' test cases, totaling 6 test cases.
2231
2332
Answer in JSON format:
2433
{{
2534
"test_cases": [
26-
{{"description": "<describe test case 1 in {chat_language}>"}},
27-
{{"description": "<describe test case 2 in {chat_language}>"}},
35+
{{"description": "<describe test case 1 in {chat_language}>", "category": "happy path"}},
36+
...
37+
{{"description": "<describe test case 4 in {chat_language}>", "category": "edge case"}},
38+
...
2839
]
2940
}}
3041
"""

unit_tests/propose_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ def propose_test(
9393
descriptions = []
9494
for case in cases:
9595
description = case.get("description", None)
96+
category = case.get("category", None)
9697
if description:
97-
descriptions.append(description)
98+
if category:
99+
descriptions.append(category + ": " + description)
100+
else:
101+
descriptions.append(description)
98102

99103
return descriptions

0 commit comments

Comments
 (0)