Skip to content

Testes

Executando Testes

Todos os testes

cd ifriend_agent
pytest -v

Testes específicos

# Testes de agents
pytest tests/test_sub_agents.py -v

# Testes do orquestrador
pytest tests/test_orchestrator.py -v

# Testes de feature flags
pytest tests/test_feature_flags.py -v

# Testes A2A
pytest tests/test_a2a.py -v
pytest tests/test_a2a_client.py -v

# Testes de memória
pytest tests/test_memory_service.py -v

Estrutura de Testes

ifriend_agent/tests/
├── test_sub_agents.py      # Testes de cada sub-agent
├── test_orchestrator.py    # Testes do root_agent
├── test_feature_flags.py   # Testes das flags
├── test_agent_builder.py  # Testes do builder
├── test_memory_service.py # Testes de memória
├── test_app_config.py      # Testes de configuração
├── test_sanitize.py        # Testes de PII
├── test_a2a.py             # Testes A2A
├── test_a2a_client.py      # Testes A2A Client
└── test_loop_guard.py      # Testes de proteção anti-loop

Escrevendo Testes

Teste de Agent

import pytest
from ifriend_agent.agents.discovery_agent import discovery_agent

class TestDiscoveryAgent:
    def test_is_llm_agent(self):
        from google.adk.agents import LlmAgent
        assert isinstance(discovery_agent, LlmAgent)

    def test_name(self):
        assert discovery_agent.name == 'discovery_agent'

    def test_tools_count(self):
        assert len(discovery_agent.tools) >= 5

Teste de Tool

import pytest
from ifriend_agent.tools.minha_tool import minha_tool

class TestMinhaTool:
    @pytest.mark.asyncio
    async def test_returns_expected_format(self):
        from unittest.mock import MagicMock
        context = MagicMock()
        context.state = {}

        result = await minha_tool(context, param="test")
        assert "data" in result or "error" in result

Mockando ferramentas externas

from unittest.mock import patch, AsyncMock

@patch('google.cloud.bigquery.Client')
def test_tool_with_bq_mock(mock_bq):
    mock_bq.return_value.query.return_value = [...]
    # Teste aqui

Coverage

pytest --cov=ifriend_agent --cov-report=html

Testes de Integração

Para testes que precisam do banco real, use fixtures:

@pytest.fixture
def db_connection():
    # Setup DB
    yield connection
    # Cleanup