testing
Generate, execute, and analyze tests for codebases, covering unit, integration, and end-to-end testing with coverage reporting. Use when the user requests testing or provides relevant inputs for this workflow.
git clone --depth 1 https://github.com/seb1n/awesome-ai-agent-skills /tmp/testing && cp -r /tmp/testing/code-and-development/testing ~/.claude/skills/testingSKILL.md
# Testing
This skill enables an AI agent to systematically generate, run, and evaluate tests for a given codebase. It covers the full testing lifecycle — from analyzing source code and identifying meaningful test cases, through writing and executing tests, to measuring coverage and recommending improvements. The agent supports unit tests, integration tests, and end-to-end tests across multiple languages and frameworks.
## Workflow
1. **Analyze the source code.** Read the target file or module and build a dependency graph of its functions, classes, and external interactions. Identify public interfaces, internal helpers, input parameters, return types, and side effects. This step determines what is testable and what kinds of tests are appropriate.
2. **Identify test cases.** For each function or method, enumerate the scenarios that need coverage: happy-path inputs, boundary values, invalid or null inputs, exception paths, and state transitions. For integration points, identify the collaborators that need to be mocked or stubbed versus tested live. Prioritize cases by risk — complex branching logic and public API surfaces come first.
3. **Write the tests.** Generate well-structured test code using the project's existing test framework (e.g., pytest, Jest, JUnit). Each test should have a descriptive name that states the scenario and expected outcome. Use the Arrange-Act-Assert pattern: set up preconditions, invoke the code under test, and assert the expected result. Add parameterized tests where a single logical case applies to multiple input sets.
4. **Run the tests.** Execute the test suite using the appropriate runner command. Capture the full output including pass/fail status, assertion messages, and timing information. If any tests fail, parse the failure output to determine whether the failure indicates a bug in the source code or an error in the test itself.
5. **Analyze coverage.** Run the test suite with coverage instrumentation enabled (e.g., `pytest --cov`, `jest --coverage`). Parse the coverage report to identify uncovered lines, branches, and functions. Flag any critical code paths — error handlers, security checks, data validation — that lack coverage.
6. **Suggest improvements.** Based on coverage gaps and code complexity, recommend additional test cases. Suggest refactoring opportunities that would make the code more testable, such as extracting pure functions or introducing dependency injection. Provide a summary report with coverage percentages and a prioritized list of next actions.
## Supported Languages
| Language | Framework | Runner Command |
|------------|-----------------|---------------------------------|
| Python | pytest | `pytest --cov=src -v` |
| JavaScript | Jest | `npx jest --coverage --verbose` |
| TypeScript | Jest / Vitest | `npx vitest run --coverage` |
| Java | JUnit 5 | `mvn test` |
| Go | testing (stdlib) | `go test -cover ./...` |
| Rust | cargo test | `cargo test` |
## Usage
Provide one or more of the following inputs:
- **Source file or directory** to generate tests for (e.g., `src/utils/parser.py`).
- **Existing test file** if you want the agent to extend or improve current tests.
- **Test framework preference** if the project does not already have one configured.
- **Coverage threshold** if you want the agent to target a specific percentage (e.g., 90%).
## Examples
### Example 1 — Python with pytest
Given this source file `src/cart.py`:
```python
class ShoppingCart:
def __init__(self):
self.items = []
def add_item(self, name: str, price: float, quantity: int = 1):
if price < 0:
raise ValueError("Price cannot be negative")
if quantity < 1:
raise ValueError("Quantity must be at least 1")
self.items.append({"name": name, "price": price, "quantity": quantity})
def total(self) -> float:
return sum(item["price"] * item["quantity"] for item in self.items)
def remove_item(self, name: str):
self.items = [item for item in self.items if item["name"] != name]
```
The agent generates `tests/test_cart.py`:
```python
import pytest
from src.cart import ShoppingCart
@pytest.fixture
def cart():
return ShoppingCart()
def test_add_item_and_total(cart):
cart.add_item("Widget", 9.99, 2)
assert cart.total() == pytest.approx(19.98)
def test_empty_cart_total_is_zero(cart):
assert cart.total() == 0.0
def test_add_item_negative_price_raises(cart):
with pytest.raises(ValueError, match="Price cannot be negative"):
cart.add_item("Bad", -1.0)
def test_add_item_zero_quantity_raises(cart):
with pytest.raises(ValueError, match="Quantity must be at least 1"):
cart.add_item("Bad", 5.0, 0)
def test_remove_item(cart):
cart.add_item("A", 1.0)
cart.add_item("B", 2.0)
cart.remove_item("A")
assert cart.total() == 2.0
def test_remove_nonexistent_item_does_nothing(cart):
cart.add_item("A", 1.0)
cart.remove_item("Z")
assert cart.total() == 1.0
```
Run: `pytest tests/test_cart.py --cov=src -v`
### Example 2 — JavaScript with Jest
Given this source file `src/validator.js`:
```javascript
function isValidEmail(email) {
if (typeof email !== "string") return false;
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function isStrongPassword(password) {
if (typeof password !== "string") return false;
return (
password.length >= 8 &&
/[A-Z]/.test(password) &&
/[a-z]/.test(password) &&
/[0-9]/.test(password)
);
}
module.exports = { isValidEmail, isStrongPassword };
```
The agent generates `src/__tests__/validator.test.js`:
```javascript
const { isValidEmail, isStrongPassword } = require("../validator");
describe("isValidEmail", () => {
test.each([
["user@example.com", true],
["name+tag@sub.domain.org", true],
[Design reproducible evaluations for AI agents with representative task sets, explicit rubrics, appropriate graders, baselines, regression gates, and failure analysis. Use when defining agent quality, comparing prompts or models, validating a release, measuring tool-use reliability, investigating regressions, or deciding whether an agent is ready for production.
Design privacy-aware observability for AI agents using traces, spans, structured events, metrics, cost attribution, dashboards, alerts, and investigation workflows. Use when instrumenting an agent, debugging intermittent tool or model failures, defining service-level objectives, analyzing latency or spend, auditing agent decisions, or preparing production monitoring.
Design and verify auditable human oversight, approval gates, escalation paths, and safe state transitions for AI agent workflows. Use when deciding which agent actions require review, adding approve/reject or dual-control flows, preventing unauthorized autonomous effects, creating decision records, reducing rubber-stamping, or recovering safely from rejected, expired, or failed actions.
Design, implement, harden, and verify Model Context Protocol (MCP) servers with precise tool contracts, least-privilege authorization, safe transports, structured errors, and interoperability tests. Use when creating a new MCP server, exposing an API or data source through MCP, reviewing an MCP server design, adding or revising MCP tools, or preparing an MCP server for production.
Design and operate bounded multi-agent workflows with task decomposition, dependency graphs, ownership, handoff contracts, shared-state controls, approvals, recovery, and synthesis. Use when a task contains genuinely independent workstreams, specialized roles, parallel research or implementation, reviewer-worker loops, or coordination problems that one agent should not execute sequentially.
Design and validate model-facing tool definitions with clear names, action-oriented descriptions, bounded JSON Schema parameters, explicit side effects, safe defaults, idempotency, errors, and realistic tests. Use when creating function-calling tools, MCP tools, agent actions, structured tool inputs, or when a model selects the wrong tool, invents arguments, or causes unsafe side effects.
Plan, execute, document, and retest authorized security assessments of AI agents and multi-agent workflows using safe adversarial cases, synthetic identities, canaries, and evidence-based findings. Use when defining red-team rules of engagement, assessing prompt injection or excessive agency, testing tool and identity boundaries, evaluating memory or cross-agent attacks, scoring a campaign, or verifying remediation in an approved environment.
Threat-model and harden AI agents, RAG systems, assistants, and tool-using workflows against direct, indirect, stored, cross-agent, and multimodal prompt injection. Use when reviewing an agent architecture, isolating untrusted content, constraining tools and egress, protecting secrets, adding injection-focused tests, investigating a suspected injection incident, or documenting residual prompt-injection risk.