feat: implement pytest plugin for AgentUnit scenario discovery (resolves #22)#38
Closed
sshekhar563 wants to merge 10 commits into
Closed
Conversation
- Create tests/integration/ directory with comprehensive LangGraph tests - Add simple_langgraph_agent.py with mock LangGraph agent for testing - Implement test_langgraph_integration.py with full evaluation cycle tests - Configure pytest markers for integration and langgraph tests - Add graceful dependency handling - tests skip when LangGraph not installed - Include comprehensive documentation and CI examples - Update main README with integration test instructions Fixes aviralgarg05#24
- Add langgraph as optional dependency in pyproject.toml - Update installation instructions to use poetry extras - Ensure LangGraph is properly managed within Poetry environment - Address coderabbit review feedback about pip vs poetry installation
- Update actions/setup-python from v4 to v5 - Update codecov/codecov-action from v3 to v4 - Address coderabbit security and compatibility recommendations
- Correct indentation for steps sections in both jobs - Ensure proper YAML syntax for GitHub Actions workflow - Address coderabbit YAML formatting feedback
- Update langchain version constraint to be compatible with langgraph - Resolve langchain-core version conflicts between langchain and langgraph - Update poetry.lock with resolved dependencies - Fix CI test failures caused by outdated lock file
- Change absolute imports to relative imports in test files - Add missing tests/__init__.py for proper package structure - Fix all linting errors (whitespace, formatting, imports) - Improve code quality in simple_langgraph_agent.py - Resolve ModuleNotFoundError issues in CI This addresses the test import failures and ensures proper Python package structure for the tests directory.
- Add proper __init__ method that calls super() with required name and loader - Change iter_cases to _generate_cases method that returns a list - This fixes the TypeError about missing positional arguments Resolves the 8 failing LangGraph integration tests.
- Replace invalid 'response_length' metric with 'faithfulness' metric - Fix missing newline at end of tests/__init__.py file - This should resolve the last failing test and linting issues All LangGraph integration tests should now pass successfully.
- Run ruff format on all integration test files - Fix formatting for conftest.py, simple_langgraph_agent.py, test_integration_basic.py, and test_langgraph_integration.py - This resolves the 'Run ruff formatter check' CI failure All linting and formatting checks should now pass.
Resolves aviralgarg05#22 This commit implements a comprehensive pytest plugin that enables automatic discovery and execution of AgentUnit scenarios as pytest tests. ## Features Added: ### Core Plugin Functionality: - Automatic scenario discovery from tests/eval/ directory - Support for Python files with Scenario objects and scenario_* functions - Support for YAML/JSON config files (with nocode module integration) - Pytest markers (@pytest.mark.agentunit, @pytest.mark.scenario) - Proper test execution using AgentUnit's run_suite function - Comprehensive error handling for failed scenario loading ### CLI Tool: - agentunit-init-eval command for directory setup - Generates example scenario files with correct API usage - Supports custom directory names and example creation ### Files Added: - src/agentunit/pytest/plugin.py - Main plugin implementation - src/agentunit/pytest/cli.py - CLI command for setup - src/agentunit/pytest/__init__.py - Package initialization - tests/test_pytest_plugin.py - Comprehensive test suite (6 tests) - tests/eval/example_scenarios.py - Example scenarios - docs/pytest-plugin.md - Complete documentation ### Configuration: - Added pytest entry point in pyproject.toml - Plugin auto-registers when AgentUnit is installed ## API Corrections: - Updated all examples to use 'adapter' parameter instead of deprecated 'agent' - Created SimpleAdapter class for function-based agents - Fixed CLI-generated examples to use proper adapter pattern ## Testing: - All 6 plugin tests pass - Comprehensive test coverage for discovery, execution, and error handling - Code passes ruff formatting and linting - No type checking diagnostics ## Usage: 1. Install AgentUnit (plugin auto-registers) 2. Run: agentunit-init-eval -d tests/eval -e 3. Create scenario files in tests/eval/ 4. Run: pytest tests/eval/ 5. Filter with: pytest -m agentunit The plugin integrates seamlessly with pytest's discovery mechanism and provides a natural way to run AgentUnit evaluations as part of test suites.
Learn moreAll Green is an AI agent that automatically: ✅ Addresses code review comments ✅ Fixes failing CI checks ✅ Resolves merge conflicts |
|
Caution Review failedThe pull request is closed. WalkthroughThis PR implements a comprehensive pytest plugin for AgentUnit that enables pytest-based scenario discovery and execution. It adds pytest hooks for collecting AgentUnit scenarios from Python files and YAML/JSON configs in a tests/eval/ directory, CLI tooling for scenario initialization, LangGraph integration tests, and updated documentation. Changes
Sequence DiagramsequenceDiagram
actor User
participant Pytest
participant PluginHooks
participant AgentUnitFile
participant ScenarioLoader
participant AgentUnitCore
participant FileSystem
User->>Pytest: pytest tests/eval/
Pytest->>PluginHooks: pytest_configure()
PluginHooks->>Pytest: Register markers
Pytest->>PluginHooks: pytest_collect_file(path)
PluginHooks->>FileSystem: Check if tests/eval/ dir
FileSystem-->>PluginHooks: Path valid
PluginHooks->>AgentUnitFile: Create from_parent()
Pytest->>AgentUnitFile: collect()
AgentUnitFile->>ScenarioLoader: _discover_scenarios()
ScenarioLoader->>FileSystem: Read .py/.yaml/.json files
FileSystem-->>ScenarioLoader: File content
alt Python File
ScenarioLoader->>ScenarioLoader: _discover_python_scenarios()
ScenarioLoader->>ScenarioLoader: _import_module()
else Config File
ScenarioLoader->>ScenarioLoader: _discover_config_scenarios()
end
ScenarioLoader-->>AgentUnitFile: list[Scenario]
loop For Each Scenario
AgentUnitFile->>AgentUnitFile: Create AgentUnitItem
end
AgentUnitFile-->>Pytest: Generator[AgentUnitItem]
Pytest->>AgentUnitFile: runtest()
AgentUnitFile->>AgentUnitCore: run_suite(scenario)
AgentUnitCore-->>AgentUnitFile: RunResult
alt Success
AgentUnitFile-->>Pytest: Test passes
else Failure
AgentUnitFile->>Pytest: Raise AssertionError with details
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related issues
Possibly related PRs
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (19)
Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

🎯 Overview #22
This PR implements a comprehensive pytest plugin that enables automatic discovery and execution of AgentUnit scenarios as pytest tests, resolving issue #22.
✨ Features Added
🔍 Core Plugin Functionality
tests/eval/directoryScenarioobjects andscenario_*functions@pytest.mark.agentunit,@pytest.mark.scenario)run_suitefunction🛠️ CLI Tool
agentunit-init-evalcommand for quick setup📁 Files Added
src/agentunit/pytest/plugin.py- Main plugin implementationsrc/agentunit/pytest/cli.py- CLI setup commandsrc/agentunit/pytest/__init__.py- Package initializationtests/test_pytest_plugin.py- Comprehensive test suite (6 tests)tests/eval/example_scenarios.py- Working example scenariosdocs/pytest-plugin.md- Complete documentation⚙️ Configuration
pyproject.toml🔧 API Corrections
adapterparameter instead of deprecatedagentSimpleAdapterclass for function-based agents🧪 Testing & Quality
📖 Usage Example
1. Initialize evaluation directory: