Testing MicroAgent Configuration and Initialization in OpenHands
This test suite validates the MicroAgent utility functionality in the OpenHands project, focusing on proper agent initialization and environment configuration. The tests ensure correct loading of agent files and environment variable handling.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
all-hands-ai/openhands
tests/unit/test_microagent_utils.py
import os
from pytest import MonkeyPatch
import openhands.agenthub # noqa: F401
from openhands.utils.microagent import MicroAgent
CONTENT = (
'# dummy header
' 'dummy content
' '## dummy subheader
' 'dummy subcontent
'
)
def test_micro_agent_load(tmp_path, monkeypatch: MonkeyPatch):
with open(os.path.join(tmp_path, 'dummy.md'), 'w') as f:
f.write(
(
'---
'
'name: dummy
'
'agent: CodeActAgent
'
'require_env_var:
'
' SANDBOX_OPENHANDS_TEST_ENV_VAR: "Set this environment variable for testing purposes"
'
'---
' + CONTENT
)
)
# Patch the required environment variable
monkeypatch.setenv('SANDBOX_OPENHANDS_TEST_ENV_VAR', 'dummy_value')
micro_agent = MicroAgent(os.path.join(tmp_path, 'dummy.md'))
assert micro_agent is not None
assert micro_agent.content == CONTENT.strip()