Skip to content

Commit 0c1ff5d

Browse files
committed
Adjust test_prompt_generator and add test report generation
1 parent 5dfdb2e commit 0c1ff5d

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

tests.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import unittest
2+
import coverage
23

34
if __name__ == "__main__":
5+
# Start coverage collection
6+
cov = coverage.Coverage()
7+
cov.start()
8+
49
# Load all tests from the 'autogpt/tests' package
5-
suite = unittest.defaultTestLoader.discover("autogpt/tests")
10+
suite = unittest.defaultTestLoader.discover("./tests")
611

712
# Run the tests
813
unittest.TextTestRunner().run(suite)
14+
15+
# Stop coverage collection
16+
cov.stop()
17+
cov.save()
18+
19+
# Report the coverage
20+
cov.report(show_missing=True)
Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
# Import the required libraries for unit testing
2-
import os
3-
import sys
4-
import unittest
1+
from unittest import TestCase
52

63
from autogpt.promptgenerator import PromptGenerator
74

85

9-
# Create a test class for the PromptGenerator, subclassed from unittest.TestCase
10-
class promptgenerator_tests(unittest.TestCase):
11-
# Set up the initial state for each test method by creating an instance of PromptGenerator
12-
def setUp(self):
13-
self.generator = PromptGenerator()
6+
class TestPromptGenerator(TestCase):
7+
"""
8+
Test cases for the PromptGenerator class, which is responsible for generating
9+
prompts for the AI with constraints, commands, resources, and performance evaluations.
10+
"""
11+
12+
@classmethod
13+
def setUpClass(cls):
14+
"""
15+
Set up the initial state for each test method by creating an instance of PromptGenerator.
16+
"""
17+
cls.generator = PromptGenerator()
1418

1519
# Test whether the add_constraint() method adds a constraint to the generator's constraints list
1620
def test_add_constraint(self):
21+
"""
22+
Test if the add_constraint() method adds a constraint to the generator's constraints list.
23+
"""
1724
constraint = "Constraint1"
1825
self.generator.add_constraint(constraint)
1926
self.assertIn(constraint, self.generator.constraints)
2027

2128
# Test whether the add_command() method adds a command to the generator's commands list
2229
def test_add_command(self):
30+
"""
31+
Test if the add_command() method adds a command to the generator's commands list.
32+
"""
2333
command_label = "Command Label"
2434
command_name = "command_name"
2535
args = {"arg1": "value1", "arg2": "value2"}
@@ -31,20 +41,29 @@ def test_add_command(self):
3141
}
3242
self.assertIn(command, self.generator.commands)
3343

34-
# Test whether the add_resource() method adds a resource to the generator's resources list
3544
def test_add_resource(self):
45+
"""
46+
Test if the add_resource() method adds a resource to the generator's resources list.
47+
"""
3648
resource = "Resource1"
3749
self.generator.add_resource(resource)
3850
self.assertIn(resource, self.generator.resources)
3951

40-
# Test whether the add_performance_evaluation() method adds an evaluation to the generator's performance_evaluation list
4152
def test_add_performance_evaluation(self):
53+
"""
54+
Test if the add_performance_evaluation() method adds an evaluation to the generator's
55+
performance_evaluation list.
56+
"""
4257
evaluation = "Evaluation1"
4358
self.generator.add_performance_evaluation(evaluation)
4459
self.assertIn(evaluation, self.generator.performance_evaluation)
4560

46-
# Test whether the generate_prompt_string() method generates a prompt string with all the added constraints, commands, resources and evaluations
4761
def test_generate_prompt_string(self):
62+
"""
63+
Test if the generate_prompt_string() method generates a prompt string with all the added
64+
constraints, commands, resources, and evaluations.
65+
"""
66+
# Define the test data
4867
constraints = ["Constraint1", "Constraint2"]
4968
commands = [
5069
{
@@ -61,7 +80,7 @@ def test_generate_prompt_string(self):
6180
resources = ["Resource1", "Resource2"]
6281
evaluations = ["Evaluation1", "Evaluation2"]
6382

64-
# Add all the constraints, commands, resources, and evaluations to the generator
83+
# Add test data to the generator
6584
for constraint in constraints:
6685
self.generator.add_constraint(constraint)
6786
for command in commands:
@@ -76,24 +95,20 @@ def test_generate_prompt_string(self):
7695
# Generate the prompt string and verify its correctness
7796
prompt_string = self.generator.generate_prompt_string()
7897
self.assertIsNotNone(prompt_string)
98+
99+
# Check if all constraints, commands, resources, and evaluations are present in the prompt string
79100
for constraint in constraints:
80101
self.assertIn(constraint, prompt_string)
81102
for command in commands:
82103
self.assertIn(command["name"], prompt_string)
83-
84-
# Check for each key-value pair in the command args dictionary
85-
for key, value in command["args"].items():
86-
self.assertIn(f'"{key}": "{value}"', prompt_string)
104+
for key, value in command["args"].items():
105+
self.assertIn(f'"{key}": "{value}"', prompt_string)
87106
for resource in resources:
88107
self.assertIn(resource, prompt_string)
89108
for evaluation in evaluations:
90109
self.assertIn(evaluation, prompt_string)
110+
91111
self.assertIn("constraints", prompt_string.lower())
92112
self.assertIn("commands", prompt_string.lower())
93113
self.assertIn("resources", prompt_string.lower())
94114
self.assertIn("performance evaluation", prompt_string.lower())
95-
96-
97-
# Run the tests when this script is executed
98-
if __name__ == "__main__":
99-
unittest.main()

0 commit comments

Comments
 (0)