conftest.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import os
  2. import pytest
  3. import subprocess
  4. import logging
  5. import shutil
  6. import datetime
  7. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  8. CASES_DIR = os.path.join(SCRIPT_DIR, 'cases')
  9. AGENTHUB_DIR = os.path.join(SCRIPT_DIR, '../../', 'agenthub')
  10. def agents():
  11. """Retrieves a list of available agents.
  12. Returns:
  13. A list of agent names.
  14. """
  15. agents = []
  16. for agent in os.listdir(AGENTHUB_DIR):
  17. if os.path.isdir(os.path.join(AGENTHUB_DIR, agent)) and agent.endswith('_agent'):
  18. agents.append(agent)
  19. return agents
  20. @pytest.fixture(scope='session')
  21. def test_cases_dir():
  22. """Fixture that provides the directory path for test cases.
  23. Returns:
  24. The directory path for test cases.
  25. """
  26. return CASES_DIR
  27. @pytest.fixture
  28. def task_file(test_cases_dir, request):
  29. """Fixture that provides the path to the task file for a test case.
  30. Args:
  31. test_cases_dir: The directory path for test cases.
  32. request: The pytest request object.
  33. Returns:
  34. The path to the task file for the test case.
  35. """
  36. test_case_dir = os.path.dirname(request.module.__file__)
  37. task_file_path = os.path.join(test_case_dir, 'task.txt')
  38. return task_file_path
  39. @pytest.fixture
  40. def workspace_dir(test_cases_dir, request):
  41. """Fixture that provides the workspace directory for a test case.
  42. Args:
  43. test_cases_dir: The directory path for test cases.
  44. request: The pytest request object.
  45. Returns:
  46. The workspace directory for the test case.
  47. """
  48. test_case_dir = os.path.dirname(request.module.__file__)
  49. workspace_dir = os.path.join(test_case_dir, 'workspace')
  50. return workspace_dir
  51. @pytest.fixture
  52. def model(request):
  53. """Fixture that provides the model name.
  54. Args:
  55. request: The pytest request object.
  56. Returns:
  57. The model name, defaulting to "gpt-3.5-turbo-1106".
  58. """
  59. return request.config.getoption('model', default='gpt-3.5-turbo-1106')
  60. @pytest.fixture
  61. def run_test_case(test_cases_dir, workspace_dir, request):
  62. """Fixture that provides a function to run a test case.
  63. Args:
  64. test_cases_dir: The directory path for test cases.
  65. workspace_dir: The workspace directory for the test case.
  66. request: The pytest request object.
  67. Returns:
  68. A function that runs a test case for a given agent and case.
  69. """
  70. def _run_test_case(agent, case):
  71. """Runs a test case for a given agent.
  72. Args:
  73. agent: The name of the agent to run the test case for.
  74. case: The name of the test case to run.
  75. Returns:
  76. The path to the workspace directory for the agent and test case.
  77. Raises:
  78. AssertionError: If the test case execution fails (non-zero return code).
  79. Steps:
  80. """
  81. case_dir = os.path.join(test_cases_dir, case)
  82. task = open(os.path.join(case_dir, 'task.txt'), 'r').read().strip()
  83. outputs_dir = os.path.join(case_dir, 'outputs')
  84. agent_dir = os.path.join(outputs_dir, agent)
  85. if not os.path.exists(agent_dir):
  86. os.makedirs(agent_dir)
  87. shutil.rmtree(os.path.join(agent_dir, 'workspace'), ignore_errors=True)
  88. if os.path.isdir(os.path.join(case_dir, 'start')):
  89. os.copytree(os.path.join(case_dir, 'start'), os.path.join(agent_dir, 'workspace'))
  90. else:
  91. os.makedirs(os.path.join(agent_dir, 'workspace'))
  92. agents_ref = {
  93. 'monologue_agent':'MonologueAgent',
  94. 'codeact_agent':'CodeActAgent'
  95. }
  96. process = subprocess.Popen(['python3', f'{SCRIPT_DIR}/../../opendevin/main.py', '-d', f"{os.path.join(agent_dir, 'workspace')}", '-c', f'{agents_ref[agent]}', '-t', f'{task}', '-m', 'gpt-3.5-turbo-1106'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  97. stdout, stderr = process.communicate()
  98. logging.info(f'Stdout: {stdout}')
  99. logging.error(f'Stderr: {stderr}')
  100. assert process.returncode == 0
  101. return os.path.join(agent_dir, 'workspace')
  102. return _run_test_case
  103. def pytest_configure(config):
  104. """Configuration hook for pytest.
  105. Args:
  106. config: The pytest configuration object.
  107. """
  108. now = datetime.datetime.now()
  109. logging.basicConfig(
  110. level=logging.INFO,
  111. format='%(asctime)s [%(levelname)s] %(message)s',
  112. handlers=[
  113. logging.FileHandler(f"test_results_{now.strftime('%Y%m%d_%H%M%S')}.log"),
  114. logging.StreamHandler()
  115. ]
  116. )