multi-agent-code-assistant / tester_agent.py
Navya-Sree's picture
Create tester_agent.py
7bdd865 verified
raw
history blame
1.4 kB
import subprocess
import tempfile
import os
def test_agent(code, test_inputs=None):
"""
Test the generated code with given test inputs.
For simplicity, we assume the code is a function and we have a test harness.
We'll create a temporary test file and run pytest.
"""
if test_inputs is None:
# Default test: check if the function runs without error
test_inputs = [("test_input", "expected_output")]
# We'll create a temporary test file
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(code)
f.write("\n\n")
# Write a simple test function (this is a very basic example)
f.write("def test_generated_code():\n")
for input_val, expected in test_inputs:
# This is a very naive way, we assume the function is named 'foo'
# In reality, we need to know the function name and its signature
f.write(f" try:\n")
f.write(f" result = foo('{input_val}')\n")
f.write(f" assert result == '{expected}'\n")
f.write(f" except Exception as e:\n")
f.write(f" assert False, f'Error: {e}'\n")
f.flush()
# Now run pytest on this file
result = subprocess.run(['pytest', f.name], capture_output=True, text=True)
os.unlink(f.name)
return result.stdout