Spaces:
Runtime error
Runtime error
import unittest | |
import sys | |
import io | |
import contextlib | |
import os | |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
def run_tests(test_code): | |
"""Execute the generated test cases.""" | |
try: | |
# Redirect stdout to capture test results | |
with io.StringIO() as buf, contextlib.redirect_stdout(buf): | |
# Execute the test code | |
exec(test_code, globals()) | |
# Find test suite and run it | |
for var_name, var_value in globals().items(): | |
if isinstance(var_value, type) and issubclass( | |
var_value, unittest.TestCase): | |
suite = unittest.TestLoader().loadTestsFromTestCase(var_value) | |
unittest.TextTestRunner(stream=buf, verbosity=2).run(suite) | |
test_output = buf.getvalue() | |
print(test_output) | |
return 0 # Return 0 to indicate successful execution | |
except Exception as e: | |
print(f"Error executing tests: {str(e)}") | |
return 1 # Return 1 to indicate an error | |
if __name__ == "__main__": | |
test_code = sys.stdin.read() | |
exit_code = run_tests(test_code) | |
sys.exit(exit_code) |