Spaces:
Build error
Build error
Create tester_agent.py
Browse files- tester_agent.py +35 -0
tester_agent.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def test_agent(code, test_inputs=None):
|
| 6 |
+
"""
|
| 7 |
+
Test the generated code with given test inputs.
|
| 8 |
+
For simplicity, we assume the code is a function and we have a test harness.
|
| 9 |
+
We'll create a temporary test file and run pytest.
|
| 10 |
+
"""
|
| 11 |
+
if test_inputs is None:
|
| 12 |
+
# Default test: check if the function runs without error
|
| 13 |
+
test_inputs = [("test_input", "expected_output")]
|
| 14 |
+
|
| 15 |
+
# We'll create a temporary test file
|
| 16 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 17 |
+
f.write(code)
|
| 18 |
+
f.write("\n\n")
|
| 19 |
+
# Write a simple test function (this is a very basic example)
|
| 20 |
+
f.write("def test_generated_code():\n")
|
| 21 |
+
for input_val, expected in test_inputs:
|
| 22 |
+
# This is a very naive way, we assume the function is named 'foo'
|
| 23 |
+
# In reality, we need to know the function name and its signature
|
| 24 |
+
f.write(f" try:\n")
|
| 25 |
+
f.write(f" result = foo('{input_val}')\n")
|
| 26 |
+
f.write(f" assert result == '{expected}'\n")
|
| 27 |
+
f.write(f" except Exception as e:\n")
|
| 28 |
+
f.write(f" assert False, f'Error: {e}'\n")
|
| 29 |
+
f.flush()
|
| 30 |
+
|
| 31 |
+
# Now run pytest on this file
|
| 32 |
+
result = subprocess.run(['pytest', f.name], capture_output=True, text=True)
|
| 33 |
+
os.unlink(f.name)
|
| 34 |
+
|
| 35 |
+
return result.stdout
|