Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions openagent_eval/cicd/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ def test_rag_evaluation():
from __future__ import annotations

import asyncio
import sys
import time
from pathlib import Path
from typing import Any, Generator

import pytest

from openagent_eval.cicd.models import CICDConfig, EvaluationGate, ThresholdConfig
from openagent_eval.cicd.thresholds import EvaluationResult, ThresholdEvaluator

Expand Down Expand Up @@ -89,6 +88,8 @@ def pytest_collection_modifyitems(
config: pytest.Config, items: list[pytest.Item]
) -> None:
"""Modify collected items to add OpenAgent Eval markers."""
import pytest

for item in items:
if "oaeval" in item.keywords:
item.add_marker(pytest.mark.oaeval)
Expand Down Expand Up @@ -263,7 +264,6 @@ def pytest_runtest_makereport(
report.oaeval_result = item._oaeval_result # type: ignore[attr-defined]


@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item: pytest.Item) -> None:
"""Setup hook for OpenAgent Eval tests."""
# Check if this is an oaeval test
Expand All @@ -272,6 +272,14 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
item._is_oaeval_test = True # type: ignore[attr-defined]


# Apply pytest hook decorator only when pytest is available. This keeps the
# module importable on clean installs that do not include pytest.
if sys.modules.get("pytest") is not None:
import pytest

pytest_runtest_setup = pytest.hookimpl(tryfirst=True)(pytest_runtest_setup)


def pytest_sessionfinish(
session: pytest.Session, exitstatus: int
) -> None:
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_cicd/test_no_pytest_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Regression test for issue #227.

The CLI must be importable and runnable on a clean install that does not
include pytest. We verify this in-process by blocking the ``pytest`` name
before importing the modules.
"""

import subprocess
import sys


def test_cicd_and_cli_import_without_pytest() -> None:
"""Plugin and CLI entry point import cleanly when pytest is unavailable."""
code = (
"import sys\n"
"sys.modules['pytest'] = None\n"
"from openagent_eval.cicd import OAEvalPlugin\n"
"from openagent_eval.cli.main import app\n"
"print('import-ok')\n"
)
result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
assert "import-ok" in result.stdout


def test_oaeval_help_runs_without_pytest() -> None:
"""``oaeval --help`` works when pytest is unavailable."""
code = (
"import sys\n"
"sys.modules['pytest'] = None\n"
"from typer.testing import CliRunner\n"
"from openagent_eval.cli.main import app\n"
"runner = CliRunner()\n"
"result = runner.invoke(app, ['--help'])\n"
"print(result.output)\n"
"assert result.exit_code == 0, result.exception\n"
)
result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
assert "Usage:" in result.stdout
Loading