pytest-agent-bdd 0.1.0.dev1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_agent_bdd-0.1.0.dev1/.github/ISSUE_TEMPLATE/integration.md +23 -0
- pytest_agent_bdd-0.1.0.dev1/.github/workflows/ci.yml +21 -0
- pytest_agent_bdd-0.1.0.dev1/.github/workflows/deploy-pypi.yml +36 -0
- pytest_agent_bdd-0.1.0.dev1/.gitignore +45 -0
- pytest_agent_bdd-0.1.0.dev1/CHANGELOG.md +17 -0
- pytest_agent_bdd-0.1.0.dev1/CONTRIBUTING.md +39 -0
- pytest_agent_bdd-0.1.0.dev1/LICENSE +21 -0
- pytest_agent_bdd-0.1.0.dev1/PKG-INFO +256 -0
- pytest_agent_bdd-0.1.0.dev1/README.md +222 -0
- pytest_agent_bdd-0.1.0.dev1/docs/architecture.md +99 -0
- pytest_agent_bdd-0.1.0.dev1/docs/contributors.md +90 -0
- pytest_agent_bdd-0.1.0.dev1/docs/conversation/2026-11-26-gherkin-evaluation-contracts.md +3293 -0
- pytest_agent_bdd-0.1.0.dev1/docs/literature.md +38 -0
- pytest_agent_bdd-0.1.0.dev1/docs/problem-statement.md +78 -0
- pytest_agent_bdd-0.1.0.dev1/docs/roadmap.md +89 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/conftest.py +28 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/datasets/support_answers.jsonl +13 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/features/judge_validation.feature +16 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/features/refund.feature +41 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/keyword_judge.py +30 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/support_agent.py +79 -0
- pytest_agent_bdd-0.1.0.dev1/examples/customer-support/test_customer_support.py +3 -0
- pytest_agent_bdd-0.1.0.dev1/pyproject.toml +62 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/__init__.py +45 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/adapters.py +98 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/assertions.py +78 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/integrations/__init__.py +57 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/integrations/_dependencies.py +22 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/integrations/langgraph.py +63 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/judges/__init__.py +93 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/models.py +90 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/plugin.py +160 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/steps.py +192 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/validation/__init__.py +169 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/validation/metrics.py +68 -0
- pytest_agent_bdd-0.1.0.dev1/src/pytest_agent_bdd/validation/robustness.py +95 -0
- pytest_agent_bdd-0.1.0.dev1/tests/test_core.py +168 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Framework integration
|
|
3
|
+
about: Propose or implement an adapter for an agent framework
|
|
4
|
+
labels: integration, help wanted
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
**Framework:**
|
|
8
|
+
|
|
9
|
+
**Objective:** Translate <framework> execution into the common `AgentExecution` model.
|
|
10
|
+
|
|
11
|
+
**Scope**
|
|
12
|
+
- [ ] Implement `<Framework>Adapter.run(input) -> AgentExecution`
|
|
13
|
+
- [ ] Capture execution output
|
|
14
|
+
- [ ] Normalize tool calls where available
|
|
15
|
+
- [ ] Report framework errors through `status` / `error`
|
|
16
|
+
- [ ] Unit tests with a mocked or deterministic execution
|
|
17
|
+
|
|
18
|
+
**Acceptance criteria**
|
|
19
|
+
- [ ] No mandatory dependency added to core (extra + `require_dependency`)
|
|
20
|
+
- [ ] Runnable example
|
|
21
|
+
- [ ] Documented trace-extraction limitations
|
|
22
|
+
|
|
23
|
+
**Estimated effort:**
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: ${{ matrix.python-version }}
|
|
19
|
+
- run: pip install -e ".[dev]"
|
|
20
|
+
- run: ruff check . && ruff format --check .
|
|
21
|
+
- run: pytest
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Deploy to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
name: Build distribution
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.12"
|
|
17
|
+
- run: pip install hatch
|
|
18
|
+
- run: hatch build
|
|
19
|
+
- uses: actions/upload-artifact@v4
|
|
20
|
+
with:
|
|
21
|
+
name: dist
|
|
22
|
+
path: dist/
|
|
23
|
+
|
|
24
|
+
publish:
|
|
25
|
+
name: Publish to PyPI
|
|
26
|
+
needs: build
|
|
27
|
+
runs-on: ubuntu-latest
|
|
28
|
+
environment: pypi
|
|
29
|
+
permissions:
|
|
30
|
+
id-token: write
|
|
31
|
+
steps:
|
|
32
|
+
- uses: actions/download-artifact@v4
|
|
33
|
+
with:
|
|
34
|
+
name: dist
|
|
35
|
+
path: dist/
|
|
36
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.pyd
|
|
6
|
+
|
|
7
|
+
# Virtual environments
|
|
8
|
+
.venv/
|
|
9
|
+
venv/
|
|
10
|
+
env/
|
|
11
|
+
|
|
12
|
+
# Testing and coverage
|
|
13
|
+
.pytest_cache/
|
|
14
|
+
.coverage
|
|
15
|
+
htmlcov/
|
|
16
|
+
coverage.xml
|
|
17
|
+
|
|
18
|
+
# Type checking and linting
|
|
19
|
+
.ruff_cache/
|
|
20
|
+
.mypy_cache/
|
|
21
|
+
|
|
22
|
+
# Build and packaging
|
|
23
|
+
dist/
|
|
24
|
+
build/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# Jupyter
|
|
30
|
+
.ipynb_checkpoints/
|
|
31
|
+
|
|
32
|
+
# IDE
|
|
33
|
+
.vscode/
|
|
34
|
+
.idea/
|
|
35
|
+
*.swp
|
|
36
|
+
*.swo
|
|
37
|
+
|
|
38
|
+
# macOS
|
|
39
|
+
.DS_Store
|
|
40
|
+
.AppleDouble
|
|
41
|
+
.LSOverride
|
|
42
|
+
|
|
43
|
+
# Environment variables
|
|
44
|
+
.env
|
|
45
|
+
.env.*
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0.dev0 — unreleased
|
|
4
|
+
|
|
5
|
+
- Project created from the design conversation of 26 November 2026
|
|
6
|
+
(docs/conversation/2026-11-26-gherkin-evaluation-contracts.md).
|
|
7
|
+
- Normalized `AgentExecution` / `ToolCall` model and `AgentAdapter` protocol.
|
|
8
|
+
- `CallableAdapter` and `ToolRecorder` for custom Python agents.
|
|
9
|
+
- pytest plugin: core fixtures, `agent_integration` and `judge_datasets_dir` ini options,
|
|
10
|
+
`agent_bdd` and `judge_validation` markers.
|
|
11
|
+
- Built-in Gherkin steps for outputs, tool calls, ordering, judges and judge validation.
|
|
12
|
+
- Lazy integration loading, optional-dependency detection, entry-point discovery;
|
|
13
|
+
LangGraph adapter skeleton.
|
|
14
|
+
- Judge protocol, registry and contract checks.
|
|
15
|
+
- Judge validation: JSONL datasets, binary metrics, Cohen's kappa, per-category results,
|
|
16
|
+
class-imbalance warnings, prompt-injection and repeatability tests.
|
|
17
|
+
- Customer-support example runnable without an API key.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thanks for your interest. The most valuable contributions right now are framework adapters,
|
|
4
|
+
realistic examples, and feedback from people testing real agents.
|
|
5
|
+
|
|
6
|
+
## Development setup
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
python -m venv .venv && source .venv/bin/activate
|
|
10
|
+
pip install -e ".[dev]"
|
|
11
|
+
pytest # unit tests + the customer-support example
|
|
12
|
+
ruff check . && ruff format --check .
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Ground rules
|
|
16
|
+
|
|
17
|
+
- The core never imports an agent framework. Framework code lives in
|
|
18
|
+
`src/pytest_agent_bdd/integrations/<name>.py`, guarded by `require_dependency(...)`, and its
|
|
19
|
+
dependency is declared as an extra in `pyproject.toml`.
|
|
20
|
+
- Adapters return `AgentExecution` and report agent failures through `status`/`error`; they do
|
|
21
|
+
not raise.
|
|
22
|
+
- Steps stay thin: put logic in `assertions`, `judges` or `validation`.
|
|
23
|
+
- Every behavioral failure message must be understandable without a debugger.
|
|
24
|
+
- Judge validation data must be human-labelled. Never generate gold labels with the judge
|
|
25
|
+
being validated.
|
|
26
|
+
|
|
27
|
+
## Writing an adapter
|
|
28
|
+
|
|
29
|
+
1. Implement `run(input) -> AgentExecution` (see `integrations/langgraph.py`).
|
|
30
|
+
2. Register it in `BUILTIN_INTEGRATIONS`, or publish it from your own package under the
|
|
31
|
+
`pytest_agent_bdd.integrations` entry-point group.
|
|
32
|
+
3. Add tests with a mocked or deterministic execution, and a runnable example.
|
|
33
|
+
4. Document what your adapter cannot capture (trace, state, tool results).
|
|
34
|
+
|
|
35
|
+
## Ownership
|
|
36
|
+
|
|
37
|
+
Each integration has a maintainer who makes implementation decisions within the adapter
|
|
38
|
+
contract. Major design decisions are discussed openly in issues; see
|
|
39
|
+
[docs/architecture.md](docs/architecture.md).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bernard Paques
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pytest-agent-bdd
|
|
3
|
+
Version: 0.1.0.dev1
|
|
4
|
+
Summary: Behavioral acceptance testing for AI agents with Gherkin and pytest-bdd.
|
|
5
|
+
Project-URL: Homepage, https://github.com/bernard357/pytest-agent-bdd
|
|
6
|
+
Project-URL: Repository, https://github.com/bernard357/pytest-agent-bdd
|
|
7
|
+
Project-URL: Issues, https://github.com/bernard357/pytest-agent-bdd/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/bernard357/pytest-agent-bdd/blob/main/CHANGELOG.md
|
|
9
|
+
Author: Bernard Paques
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: acceptance-testing,ai-agents,bdd,gherkin,llm,llm-as-a-judge,pytest,pytest-bdd
|
|
13
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
14
|
+
Classifier: Framework :: Pytest
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Topic :: Software Development :: Testing :: BDD
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Requires-Dist: pytest-bdd>=7
|
|
21
|
+
Requires-Dist: pytest>=8
|
|
22
|
+
Provides-Extra: crewai
|
|
23
|
+
Requires-Dist: crewai>=0.80; extra == 'crewai'
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: hatch>=1.12; extra == 'dev'
|
|
26
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
28
|
+
Requires-Dist: twine>=5.0; extra == 'dev'
|
|
29
|
+
Provides-Extra: langgraph
|
|
30
|
+
Requires-Dist: langgraph>=0.2; extra == 'langgraph'
|
|
31
|
+
Provides-Extra: openai-agents
|
|
32
|
+
Requires-Dist: openai-agents>=0.1; extra == 'openai-agents'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# pytest-agent-bdd
|
|
36
|
+
|
|
37
|
+
**Test your AI agents like you test your software.**
|
|
38
|
+
|
|
39
|
+
Write behavioral acceptance tests for AI agents using Gherkin and pytest. Test outputs, tool
|
|
40
|
+
calls, execution traces and custom evaluation criteria — and validate the judges you rely on.
|
|
41
|
+
|
|
42
|
+
```gherkin
|
|
43
|
+
Feature: Customer support agent handles refunds
|
|
44
|
+
|
|
45
|
+
Scenario: Process an authorized refund
|
|
46
|
+
Given the agent input field "confirmed" is "yes"
|
|
47
|
+
When I ask "Please refund order ORD-123"
|
|
48
|
+
Then the agent should invoke the "process_refund" tool
|
|
49
|
+
And the "process_refund" tool should be called with "amount" equal to "100"
|
|
50
|
+
And the "check_refund_eligibility" tool should be invoked before the "process_refund" tool
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> Status: pre-alpha (0.1.0.dev1). The API is being designed in the open — see
|
|
54
|
+
> [docs/roadmap.md](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/roadmap.md).
|
|
55
|
+
|
|
56
|
+
## The problem
|
|
57
|
+
|
|
58
|
+
Testing agentic AI systems is fundamentally different from testing conventional software.
|
|
59
|
+
|
|
60
|
+
AI agents exhibit probabilistic and non-deterministic behavior, dynamically interact with
|
|
61
|
+
external tools and services, maintain state, and make decisions across multiple execution
|
|
62
|
+
steps. Validating their final responses alone is insufficient to establish that they behave
|
|
63
|
+
correctly, reliably and safely.
|
|
64
|
+
|
|
65
|
+
Existing tools address LLM evaluation, observability and benchmarking, but engineering teams
|
|
66
|
+
lack a simple, standard way to express and automate **behavioral acceptance tests** for AI
|
|
67
|
+
agents within their existing testing workflows. Agent testing ends up in ad hoc scripts,
|
|
68
|
+
isolated datasets and hand-written assertions that are hard to maintain, reproduce and run in
|
|
69
|
+
CI/CD.
|
|
70
|
+
|
|
71
|
+
pytest-agent-bdd makes agentic systems testable with established engineering practice:
|
|
72
|
+
Gherkin as the behavioral contract, pytest fixtures as dependency injection, and a small
|
|
73
|
+
adapter as the only boundary with your agent framework.
|
|
74
|
+
|
|
75
|
+
> How can we establish that an AI agent behaves correctly, not just that it produces a
|
|
76
|
+
> plausible answer?
|
|
77
|
+
|
|
78
|
+
Full statement: [docs/problem-statement.md](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/problem-statement.md).
|
|
79
|
+
|
|
80
|
+
## Installation
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install pytest-agent-bdd # core, no framework dependency
|
|
84
|
+
pip install "pytest-agent-bdd[langgraph]" # + LangGraph integration
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Your first agent acceptance test (no API key needed)
|
|
88
|
+
|
|
89
|
+
1. Wrap your agent. Any Python callable `agent(input, tools)` works; `tools` records tool calls:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
# conftest.py
|
|
93
|
+
import pytest
|
|
94
|
+
from pytest_agent_bdd import CallableAdapter
|
|
95
|
+
from my_app import support_agent
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@pytest.fixture
|
|
99
|
+
def agent():
|
|
100
|
+
return CallableAdapter(support_agent)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
2. Describe the behavior in `features/support.feature`:
|
|
104
|
+
|
|
105
|
+
```gherkin
|
|
106
|
+
Feature: Support agent
|
|
107
|
+
Scenario: Answer a refund policy question
|
|
108
|
+
When I ask "Can I get a refund within 30 days?"
|
|
109
|
+
Then the agent should respond successfully
|
|
110
|
+
And the response should mention "refund"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
3. Bind the features and run:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
# test_support.py
|
|
117
|
+
from pytest_bdd import scenarios
|
|
118
|
+
|
|
119
|
+
scenarios("features")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pytest
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
A complete, runnable example lives in [examples/customer-support](https://github.com/bernard357/pytest-agent-bdd/blob/main/examples/customer-support).
|
|
127
|
+
|
|
128
|
+
When an assertion fails, the error shows the expectation, the agent output, the tool calls in
|
|
129
|
+
order and a trace excerpt — no need to inspect Python objects.
|
|
130
|
+
|
|
131
|
+
## Built-in fixtures
|
|
132
|
+
|
|
133
|
+
| Fixture | Purpose |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `agent` | Agent under test — override it in `conftest.py` |
|
|
136
|
+
| `agent_input` | Mutable scenario input filled by Given steps |
|
|
137
|
+
| `agent_execution` | Normalized record of the latest run (`AgentExecution`) |
|
|
138
|
+
| `agent_result` | Final output |
|
|
139
|
+
| `agent_trace` | Execution trajectory |
|
|
140
|
+
| `tool_calls` | Tool invocation records (`ToolCall`) |
|
|
141
|
+
| `agent_state` | Final agent state |
|
|
142
|
+
| `judges` | Named judge registry (optional) |
|
|
143
|
+
| `judge_validation_config` | Thresholds for judge validation (optional) |
|
|
144
|
+
|
|
145
|
+
All are function-scoped: executions, tool calls and state never leak between scenarios.
|
|
146
|
+
Your own When steps should use `target_fixture="agent_execution"`.
|
|
147
|
+
|
|
148
|
+
## Built-in steps
|
|
149
|
+
|
|
150
|
+
```gherkin
|
|
151
|
+
Given the agent input "<text>"
|
|
152
|
+
Given the agent input field "<key>" is "<value>"
|
|
153
|
+
When I ask "<text>"
|
|
154
|
+
When the agent is run
|
|
155
|
+
Then the agent should respond successfully
|
|
156
|
+
Then the response should mention "<text>"
|
|
157
|
+
Then the agent should invoke the "<tool>" tool
|
|
158
|
+
Then the agent should not invoke the "<tool>" tool
|
|
159
|
+
Then the "<tool>" tool should be called with "<arg>" equal to "<value>"
|
|
160
|
+
Then the "<first>" tool should be invoked before the "<second>" tool
|
|
161
|
+
Then no tool call should report an error
|
|
162
|
+
Then the response should pass evaluation by "<judge>" against "<expected>"
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The same checks are available in Python: `assert_tool_called`, `assert_tool_not_called`,
|
|
166
|
+
`assert_called_before`, `assert_execution_succeeded`, `assert_no_tool_errors`.
|
|
167
|
+
|
|
168
|
+
## Custom judges
|
|
169
|
+
|
|
170
|
+
A judge is anything with `evaluate(*, input, actual, expected, context=None) -> Judgment` —
|
|
171
|
+
an LLM, a Python function, an external service or a human workflow. Register judges by name:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
@pytest.fixture
|
|
175
|
+
def judges():
|
|
176
|
+
return {"semantic": MySemanticJudge()}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Every judgment is checked against the judge contract (types, score and confidence within
|
|
180
|
+
[0, 1], non-empty reason); a malformed judgment fails loudly instead of passing silently.
|
|
181
|
+
|
|
182
|
+
## Validating judge reliability
|
|
183
|
+
|
|
184
|
+
A judge that returns well-formed judgments is not necessarily a good judge. Measure it against
|
|
185
|
+
**independent, human-labelled** cases (JSONL) before trusting its verdicts:
|
|
186
|
+
|
|
187
|
+
```gherkin
|
|
188
|
+
@judge_validation
|
|
189
|
+
Feature: Judge quality
|
|
190
|
+
Background:
|
|
191
|
+
Given the "semantic" judge is configured
|
|
192
|
+
And the judge validation dataset "factuality" is loaded
|
|
193
|
+
|
|
194
|
+
Scenario: Judge agrees with human labels
|
|
195
|
+
When I validate the judge against the dataset
|
|
196
|
+
Then the judge accuracy should be at least 0.90
|
|
197
|
+
And the judge false positive rate should be below 0.05
|
|
198
|
+
|
|
199
|
+
Scenario: Judge is robust to prompt injection
|
|
200
|
+
When I run the judge robustness tests
|
|
201
|
+
Then the judge should reject manipulated evaluations
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Reports include the confusion matrix, precision, recall, F1, Cohen's kappa, per-category
|
|
205
|
+
results, class-imbalance warnings, the judge version and a content hash of the dataset.
|
|
206
|
+
|
|
207
|
+
## Running subsets
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
pytest -m agent_bdd # agent acceptance tests
|
|
211
|
+
pytest -m judge_validation # judge validation
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Framework integrations
|
|
215
|
+
|
|
216
|
+
The core imports no agent framework. Integrations are pip extras, loaded lazily:
|
|
217
|
+
|
|
218
|
+
```ini
|
|
219
|
+
[pytest]
|
|
220
|
+
agent_integration = langgraph
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
@pytest.fixture
|
|
225
|
+
def agent_under_test():
|
|
226
|
+
return build_my_graph() # the framework object the integration wraps
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Third-party packages can register adapters under the `pytest_agent_bdd.integrations`
|
|
230
|
+
entry-point group. Writing an adapter: implement `run(input) -> AgentExecution`, and report
|
|
231
|
+
agent failures through `status`/`error` rather than raising.
|
|
232
|
+
|
|
233
|
+
| Integration | Status |
|
|
234
|
+
|---|---|
|
|
235
|
+
| Custom Python agent | Built into core |
|
|
236
|
+
| LangGraph | Skeleton (first-party target) |
|
|
237
|
+
| OpenAI Agents SDK | Planned |
|
|
238
|
+
| CrewAI | Open for a community maintainer |
|
|
239
|
+
|
|
240
|
+
## Documentation
|
|
241
|
+
|
|
242
|
+
- [Problem statement](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/problem-statement.md)
|
|
243
|
+
- [Architecture and design decisions](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/architecture.md)
|
|
244
|
+
- [Roadmap, sizing and adoption plan](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/roadmap.md)
|
|
245
|
+
- [Contributor recruitment plan](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/contributors.md)
|
|
246
|
+
- [Related literature](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/literature.md)
|
|
247
|
+
- [Origin: the design conversation](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/conversation/)
|
|
248
|
+
|
|
249
|
+
## Contributing
|
|
250
|
+
|
|
251
|
+
See [CONTRIBUTING.md](https://github.com/bernard357/pytest-agent-bdd/blob/main/CONTRIBUTING.md). Framework adapters and realistic examples are the most
|
|
252
|
+
valuable contributions right now.
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
MIT
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# pytest-agent-bdd
|
|
2
|
+
|
|
3
|
+
**Test your AI agents like you test your software.**
|
|
4
|
+
|
|
5
|
+
Write behavioral acceptance tests for AI agents using Gherkin and pytest. Test outputs, tool
|
|
6
|
+
calls, execution traces and custom evaluation criteria — and validate the judges you rely on.
|
|
7
|
+
|
|
8
|
+
```gherkin
|
|
9
|
+
Feature: Customer support agent handles refunds
|
|
10
|
+
|
|
11
|
+
Scenario: Process an authorized refund
|
|
12
|
+
Given the agent input field "confirmed" is "yes"
|
|
13
|
+
When I ask "Please refund order ORD-123"
|
|
14
|
+
Then the agent should invoke the "process_refund" tool
|
|
15
|
+
And the "process_refund" tool should be called with "amount" equal to "100"
|
|
16
|
+
And the "check_refund_eligibility" tool should be invoked before the "process_refund" tool
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
> Status: pre-alpha (0.1.0.dev1). The API is being designed in the open — see
|
|
20
|
+
> [docs/roadmap.md](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/roadmap.md).
|
|
21
|
+
|
|
22
|
+
## The problem
|
|
23
|
+
|
|
24
|
+
Testing agentic AI systems is fundamentally different from testing conventional software.
|
|
25
|
+
|
|
26
|
+
AI agents exhibit probabilistic and non-deterministic behavior, dynamically interact with
|
|
27
|
+
external tools and services, maintain state, and make decisions across multiple execution
|
|
28
|
+
steps. Validating their final responses alone is insufficient to establish that they behave
|
|
29
|
+
correctly, reliably and safely.
|
|
30
|
+
|
|
31
|
+
Existing tools address LLM evaluation, observability and benchmarking, but engineering teams
|
|
32
|
+
lack a simple, standard way to express and automate **behavioral acceptance tests** for AI
|
|
33
|
+
agents within their existing testing workflows. Agent testing ends up in ad hoc scripts,
|
|
34
|
+
isolated datasets and hand-written assertions that are hard to maintain, reproduce and run in
|
|
35
|
+
CI/CD.
|
|
36
|
+
|
|
37
|
+
pytest-agent-bdd makes agentic systems testable with established engineering practice:
|
|
38
|
+
Gherkin as the behavioral contract, pytest fixtures as dependency injection, and a small
|
|
39
|
+
adapter as the only boundary with your agent framework.
|
|
40
|
+
|
|
41
|
+
> How can we establish that an AI agent behaves correctly, not just that it produces a
|
|
42
|
+
> plausible answer?
|
|
43
|
+
|
|
44
|
+
Full statement: [docs/problem-statement.md](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/problem-statement.md).
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install pytest-agent-bdd # core, no framework dependency
|
|
50
|
+
pip install "pytest-agent-bdd[langgraph]" # + LangGraph integration
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Your first agent acceptance test (no API key needed)
|
|
54
|
+
|
|
55
|
+
1. Wrap your agent. Any Python callable `agent(input, tools)` works; `tools` records tool calls:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
# conftest.py
|
|
59
|
+
import pytest
|
|
60
|
+
from pytest_agent_bdd import CallableAdapter
|
|
61
|
+
from my_app import support_agent
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@pytest.fixture
|
|
65
|
+
def agent():
|
|
66
|
+
return CallableAdapter(support_agent)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
2. Describe the behavior in `features/support.feature`:
|
|
70
|
+
|
|
71
|
+
```gherkin
|
|
72
|
+
Feature: Support agent
|
|
73
|
+
Scenario: Answer a refund policy question
|
|
74
|
+
When I ask "Can I get a refund within 30 days?"
|
|
75
|
+
Then the agent should respond successfully
|
|
76
|
+
And the response should mention "refund"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
3. Bind the features and run:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
# test_support.py
|
|
83
|
+
from pytest_bdd import scenarios
|
|
84
|
+
|
|
85
|
+
scenarios("features")
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pytest
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
A complete, runnable example lives in [examples/customer-support](https://github.com/bernard357/pytest-agent-bdd/blob/main/examples/customer-support).
|
|
93
|
+
|
|
94
|
+
When an assertion fails, the error shows the expectation, the agent output, the tool calls in
|
|
95
|
+
order and a trace excerpt — no need to inspect Python objects.
|
|
96
|
+
|
|
97
|
+
## Built-in fixtures
|
|
98
|
+
|
|
99
|
+
| Fixture | Purpose |
|
|
100
|
+
|---|---|
|
|
101
|
+
| `agent` | Agent under test — override it in `conftest.py` |
|
|
102
|
+
| `agent_input` | Mutable scenario input filled by Given steps |
|
|
103
|
+
| `agent_execution` | Normalized record of the latest run (`AgentExecution`) |
|
|
104
|
+
| `agent_result` | Final output |
|
|
105
|
+
| `agent_trace` | Execution trajectory |
|
|
106
|
+
| `tool_calls` | Tool invocation records (`ToolCall`) |
|
|
107
|
+
| `agent_state` | Final agent state |
|
|
108
|
+
| `judges` | Named judge registry (optional) |
|
|
109
|
+
| `judge_validation_config` | Thresholds for judge validation (optional) |
|
|
110
|
+
|
|
111
|
+
All are function-scoped: executions, tool calls and state never leak between scenarios.
|
|
112
|
+
Your own When steps should use `target_fixture="agent_execution"`.
|
|
113
|
+
|
|
114
|
+
## Built-in steps
|
|
115
|
+
|
|
116
|
+
```gherkin
|
|
117
|
+
Given the agent input "<text>"
|
|
118
|
+
Given the agent input field "<key>" is "<value>"
|
|
119
|
+
When I ask "<text>"
|
|
120
|
+
When the agent is run
|
|
121
|
+
Then the agent should respond successfully
|
|
122
|
+
Then the response should mention "<text>"
|
|
123
|
+
Then the agent should invoke the "<tool>" tool
|
|
124
|
+
Then the agent should not invoke the "<tool>" tool
|
|
125
|
+
Then the "<tool>" tool should be called with "<arg>" equal to "<value>"
|
|
126
|
+
Then the "<first>" tool should be invoked before the "<second>" tool
|
|
127
|
+
Then no tool call should report an error
|
|
128
|
+
Then the response should pass evaluation by "<judge>" against "<expected>"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The same checks are available in Python: `assert_tool_called`, `assert_tool_not_called`,
|
|
132
|
+
`assert_called_before`, `assert_execution_succeeded`, `assert_no_tool_errors`.
|
|
133
|
+
|
|
134
|
+
## Custom judges
|
|
135
|
+
|
|
136
|
+
A judge is anything with `evaluate(*, input, actual, expected, context=None) -> Judgment` —
|
|
137
|
+
an LLM, a Python function, an external service or a human workflow. Register judges by name:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
@pytest.fixture
|
|
141
|
+
def judges():
|
|
142
|
+
return {"semantic": MySemanticJudge()}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Every judgment is checked against the judge contract (types, score and confidence within
|
|
146
|
+
[0, 1], non-empty reason); a malformed judgment fails loudly instead of passing silently.
|
|
147
|
+
|
|
148
|
+
## Validating judge reliability
|
|
149
|
+
|
|
150
|
+
A judge that returns well-formed judgments is not necessarily a good judge. Measure it against
|
|
151
|
+
**independent, human-labelled** cases (JSONL) before trusting its verdicts:
|
|
152
|
+
|
|
153
|
+
```gherkin
|
|
154
|
+
@judge_validation
|
|
155
|
+
Feature: Judge quality
|
|
156
|
+
Background:
|
|
157
|
+
Given the "semantic" judge is configured
|
|
158
|
+
And the judge validation dataset "factuality" is loaded
|
|
159
|
+
|
|
160
|
+
Scenario: Judge agrees with human labels
|
|
161
|
+
When I validate the judge against the dataset
|
|
162
|
+
Then the judge accuracy should be at least 0.90
|
|
163
|
+
And the judge false positive rate should be below 0.05
|
|
164
|
+
|
|
165
|
+
Scenario: Judge is robust to prompt injection
|
|
166
|
+
When I run the judge robustness tests
|
|
167
|
+
Then the judge should reject manipulated evaluations
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Reports include the confusion matrix, precision, recall, F1, Cohen's kappa, per-category
|
|
171
|
+
results, class-imbalance warnings, the judge version and a content hash of the dataset.
|
|
172
|
+
|
|
173
|
+
## Running subsets
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
pytest -m agent_bdd # agent acceptance tests
|
|
177
|
+
pytest -m judge_validation # judge validation
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Framework integrations
|
|
181
|
+
|
|
182
|
+
The core imports no agent framework. Integrations are pip extras, loaded lazily:
|
|
183
|
+
|
|
184
|
+
```ini
|
|
185
|
+
[pytest]
|
|
186
|
+
agent_integration = langgraph
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
@pytest.fixture
|
|
191
|
+
def agent_under_test():
|
|
192
|
+
return build_my_graph() # the framework object the integration wraps
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Third-party packages can register adapters under the `pytest_agent_bdd.integrations`
|
|
196
|
+
entry-point group. Writing an adapter: implement `run(input) -> AgentExecution`, and report
|
|
197
|
+
agent failures through `status`/`error` rather than raising.
|
|
198
|
+
|
|
199
|
+
| Integration | Status |
|
|
200
|
+
|---|---|
|
|
201
|
+
| Custom Python agent | Built into core |
|
|
202
|
+
| LangGraph | Skeleton (first-party target) |
|
|
203
|
+
| OpenAI Agents SDK | Planned |
|
|
204
|
+
| CrewAI | Open for a community maintainer |
|
|
205
|
+
|
|
206
|
+
## Documentation
|
|
207
|
+
|
|
208
|
+
- [Problem statement](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/problem-statement.md)
|
|
209
|
+
- [Architecture and design decisions](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/architecture.md)
|
|
210
|
+
- [Roadmap, sizing and adoption plan](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/roadmap.md)
|
|
211
|
+
- [Contributor recruitment plan](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/contributors.md)
|
|
212
|
+
- [Related literature](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/literature.md)
|
|
213
|
+
- [Origin: the design conversation](https://github.com/bernard357/pytest-agent-bdd/blob/main/docs/conversation/)
|
|
214
|
+
|
|
215
|
+
## Contributing
|
|
216
|
+
|
|
217
|
+
See [CONTRIBUTING.md](https://github.com/bernard357/pytest-agent-bdd/blob/main/CONTRIBUTING.md). Framework adapters and realistic examples are the most
|
|
218
|
+
valuable contributions right now.
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
MIT
|