Back to Repositories

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

The test suite provides comprehensive coverage of MicroAgent initialization and configuration.

Key areas tested include:
  • File loading and parsing capabilities
  • Environment variable validation
  • YAML frontmatter processing
  • Content extraction functionality
Edge cases covered include proper handling of temporary file paths and environment variable requirements.

Implementation Analysis

The testing approach utilizes pytest’s powerful fixtures and monkeypatching capabilities to create isolated test environments. The implementation leverages pytest’s tmp_path fixture for temporary file operations and MonkeyPatch for environment variable manipulation.

Notable patterns include:
  • Temporary file system isolation
  • Environment variable mocking
  • Structured test data setup

Technical Details

Testing tools and configuration:
  • pytest framework for test execution
  • MonkeyPatch for environment manipulation
  • tmp_path fixture for file system operations
  • Custom YAML frontmatter parsing
  • File I/O operations testing

Best Practices Demonstrated

The test implementation showcases several testing best practices and quality patterns.

Notable practices include:
  • Isolated test environments
  • Proper fixture usage
  • Clean test data setup
  • Environment variable handling
  • Explicit assertions
  • Resource cleanup

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()