Skip to content

Add comprehensive unit tests for AWEL agent operators and context#2969

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-unit-tests-for-awel-module
Draft

Add comprehensive unit tests for AWEL agent operators and context#2969
Copilot wants to merge 3 commits intomainfrom
copilot/add-unit-tests-for-awel-module

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

Description

Added 51 unit tests (1,477 LOC) for the core AWEL agent module, which previously lacked unit test coverage. Tests cover agent context classes and AWEL operators with comprehensive scenarios including normal flows, error handling, and edge cases.

Test Files Added

  • test_agent_context.py (27 tests): AgentContext, AgentGenerateContext, AgentMessage, AgentReviewInfo

    • Initialization, serialization, message transformations
    • Context deep copying, LLM message conversion
    • Parametrized tests for common scenarios
  • test_awel_agent_operator.py (24 tests): AWELAgentOperator, WrappedAgentOperator, AgentBranchOperator

    • Normal execution and failure handling
    • Fixed subgoal processing, message role conversion
    • Branch routing logic with next_speakers
    • Agent building and memory management

Test Structure

Uses pytest with fixtures and parametrization:

@pytest.fixture
def mock_agent():
    agent = MagicMock(spec=Agent)
    agent.name = "TestAgent"
    agent.send = AsyncMock()
    return agent

@pytest.mark.asyncio
async def test_map_normal_execution(mock_agent, sample_message):
    operator = AWELAgentOperator(awel_agent=mock_awel_agent)
    result = await operator.map(input_context)
    assert result.sender == agent
    assert len(result.rely_messages) == 2

All tests use proper mocking (MagicMock, AsyncMock) to isolate dependencies and avoid external calls.

How Has This Been Tested?

Tests validated through:

  • Python syntax compilation for all test files
  • Ruff linting and formatting compliance
  • Proper import resolution and mock structure verification

Tests designed to run with: pytest tests/unit_tests/agent/ -v

Snapshots:

N/A - Unit test additions only

Checklist:

  • My code follows the style guidelines of this project
  • I have already rebased the commits and make the commit message conform to the project standard.
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • Any dependent changes have been merged and published in downstream modules
Original prompt

目标

为 DB-GPT 项目的核心 AWEL (Agentic Workflow Expression Language) 模块添加两个完整的单元测试文件,提高代码测试覆盖率。

背景

DB-GPT 是一个 AI Native 数据应用开发框架,使用 Python 开发。项目使用 pytest 作为测试框架,测试文件位于 tests/unit_tests/ 目录。当前项目中已有一些测试用例(如 tests/unit_tests/test_plugins.pytests/unit_tests/graph/test_graph.py 等),但核心的 AWEL Agent Operator 模块缺少详细的单元测试覆盖。

需要添加的测试文件

1. tests/unit_tests/agent/test_awel_agent_operator.py

测试 packages/dbgpt-core/src/dbgpt/agent/core/plan/awel/agent_operator.py 中的核心功能:

  • AWELAgentOperator 类测试:

    • 测试 map() 方法的正常执行流程
    • 测试当 input_value.already_failed 为 True 时直接返回
    • 测试消息为空时抛出异常
    • 测试 agent 构建和配置流程
    • 测试 fixed_subgoal 的处理逻辑
    • 测试消息角色转换 (HUMAN/AI)
    • 测试失败场景的处理
  • WrappedAgentOperator 类测试:

    • 测试基本的 agent 包装和消息传递
    • 测试消息内容为空时的异常处理
  • AgentBranchOperator 类测试:

    • 测试分支逻辑的判断
    • 测试根据 action_report.next_speakers 进行路由

2. tests/unit_tests/agent/test_agent_context.py

测试 packages/dbgpt-core/src/dbgpt/agent/core/agent.py 中的 Agent 上下文相关功能:

  • AgentContext 类测试:

    • 测试初始化和默认值
    • 测试 to_dict() 方法
    • 测试各种配置参数的设置
  • AgentGenerateContext 类测试:

    • 测试上下文创建和消息传递
    • 测试 rely_messages 的处理
    • 测试 already_failed 标志
    • 测试 to_dict() 方法
  • AgentMessage 类测试:

    • 测试消息的创建和复制
    • 测试 to_dict()to_llm_message() 转换
    • 测试 from_llm_message()from_messages() 工厂方法
    • 测试 action_report 的序列化
    • 测试 context 的深拷贝
  • AgentReviewInfo 类测试:

    • 测试审核信息的创建和复制
    • 测试序列化方法

��试要求

  1. 使用 pytest 框架,遵循项目现有的测试风格
  2. 使用 Mock 对象隔离外部依赖(如 LLMClient、Agent、Resource 等)
  3. 测试必须能够独立运行,不依赖外部服务或数据库
  4. 覆盖正常和异常场景,包括边界条件
  5. 使用 pytest fixtures 来设置测试数据和 mock 对象
  6. 添加清晰的文档字符串说明测试目的
  7. 使用 pytest.mark.parametrize 进行参数化测试(如适用)
  8. 确保所有测试都能通过,运行 pytest tests/unit_tests/agent/ 应该全部通过

技术参考

  • 项目使用 Python 3.10+
  • 测试框架: pytest
  • Mock 库: unittest.mock 或 pytest-mock
  • 已有的测试示例:
    • tests/unit_tests/test_plugins.py - 基本的 pytest 用法
    • tests/unit_tests/graph/test_graph.py - 使用 fixtures 和 parametrize
    • packages/dbgpt-core/src/dbgpt/agent/util/tests/test_react_parser.py - Agent 相关测试

目录结构

需要创建的文件路径:

tests/
  unit_tests/
    agent/
      __init__.py  (如果不存在则创建)
      test_awel_agent_operator.py  (新文件)
      test_agent_context.py  (新文件)

验证标准

  1. 所有测试用例都能通过运行
  2. 测试覆盖主要代码路径和边界条件
  3. Mock 使用合理,测试隔离性好
  4. 测试代码清晰易读,有良好的文档
  5. 符合项目代码风格(使用 ruff 进行格式化)

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 30, 2026 15:20
Co-authored-by: Aries-ckt <13723926+Aries-ckt@users.noreply.github.com>
Co-authored-by: Aries-ckt <13723926+Aries-ckt@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit tests for AWEL module in DB-GPT Add comprehensive unit tests for AWEL agent operators and context Jan 30, 2026
@Aries-ckt
Copy link
Collaborator

pull request -> prompt request

@Aries-ckt
Copy link
Collaborator

感谢反馈!

关于版本问题,请查看以下信息:

建议升级到最新版本以获得最新的功能和修复。

如果升级后仍有问题,请提供更多详细信息:DB-GPT 版本、操作系统、Python 版本、错误日志等。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants