Write and run tests across languages and frameworks. Use when setting up test suites, writing unit/integration/E2E tests, measuring coverage, mocking dependencies, or debugging test failures. Covers Node.js (Jest/Vitest), Python (pytest), Go, Rust, and Bash.
数据来源:ClawHub。 在 ClawSkills 查看
选择你使用的 Agent
方法一:命令行安装(推荐)
推荐(无需提前安装 clawhub)
npx clawhub@latest --dir ~/.claude/skills install test-patterns或使用 clawhub CLI(需提前安装)
clawhub --dir ~/.claude/skills install test-patterns⚠️ 需要 Node.js 18+,没有 Node?请使用下方方法二直接下载 ZIP。 安装 Node.js →
方法二:手动下载安装(无需 Node)
下载 ZIP,解压后将文件夹放到以下路径,重启 Agent 即可:
安装路径
~/.claude/skills/test-patterns/💡解压后将文件夹放到上方路径,重启 Agent 即可生效
--- name: test-patterns description: Write and run tests across languages and frameworks. Use when setting up test suites, writing unit/integration/E2E tests, measuring coverage, mocking dependencies, or debugging test failures. Covers Node.js (Jest/Vitest), Python (pytest), Go, Rust, and Bash. metadata: {"clawdbot":{"emoji":"🧪","requires":{"anyBins":["node","python3","go","cargo","bash"]},"os":["linux","darwin","win32"]}} ---
Write, run, and debug tests across languages. Covers unit tests, integration tests, E2E tests, mocking, coverage, and TDD workflows.
# Jest
npm install -D jest
# Add to package.json: "scripts": { "test": "jest" }
# Vitest (faster, ESM-native)
npm install -D vitest
# Add to package.json: "scripts": { "test": "vitest" }
// math.js
export function add(a, b) { return a + b; }
export function divide(a, b) {
if (b === 0) throw new Error('Division by zero');
return a / b;
}
// math.test.js
import { add, divide } from './math.js';
describe('add', () => {
test('adds two positive numbers', () => {
expect(add(2, 3)).toBe(5);
});
test('handles negative numbers', () => {
expect(add(-1, 1)).toBe(0);
});
test('handles zero', () => {
expect(add(0, 0)).toBe(0);
});
});
describe('divide', () => {
test('divides two numbers', () => {
expect(divide(10, 2)).toBe(5);
});
test('throws on division by zero', () => {
expect(() => divide(10, 0)).toThrow('Division by zero');
});
test('handles floating point', () => {
expect(divide(1, 3)).toBeCloseTo(0.333, 3);
});
});
// api.test.js
import { fetchUser } from './api.js';
test('fetches user by id', async () => {
const user = await fetchUser('123');
expect(user).toHaveProperty('id', '123');
expect(user).toHaveProperty('name');
expect(user.name).toBeTruthy();
});
test('throws on missing user', async () => {
await expect(fetchUser('nonexistent')).rejects.toThrow('Not found');
});
// Mock a module
jest.mock('./database.js');
import { getUser } from './database.js';
import { processUser } from './service.js';
test('processes user from database', async () => {
// Setup mock return value
getUser.mockResolvedValue({ id: '1', name: 'Alice', active: true });
const result = await processUser('1');
expect(result.processed).toBe(true);
expect(getUser).toHaveBeenCalledWith('1');
expect(getUser).toHaveBeenCalledTimes(1);
});
// Mock fetch
global.fetch = jest.fn();
test('calls API with correct params', async () => {
fetch.mockResolvedValue({
ok: true,
json: async () => ({ data: 'test' }),
});
const result = await myApiCall('/endpoint');
expect(fetch).toHaveBeenCalledWith('/endpoint', expect.objectContaining({
method: 'GET',
}));
});
// Spy on existing method (don't replace, just observe)
const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
// ... run code ...
expect(consoleSpy).toHaveBeenCalledWith('expected message');
consoleSpy.mockRestore();
# Jest
npx jest --coverage
# Vitest
npx vitest --coverage
# Check coverage thresholds (jest.config.js)
# coverageThreshold: { global: { branches: 80, functions: 80, lines: 80, statements: 80 } }
pip install pytest pytest-cov
# calculator.py
def add(a, b):
return a + b
def divide(a, b):
if b == 0:
raise ValueError("Division by zero")
return a / b
# test_calculator.py
import pytest
from calculator import add, divide
def test_add():
assert add(2, 3) == 5
def test_add_negative():
assert add(-1, 1) == 0
def test_divide():
assert divide(10, 2) == 5.0
def test_divide_by_zero():
with pytest.raises(ValueError, match="Division by zero"):
divide(10, 0)
def test_divide_float():
assert divide(1, 3) == pytest.approx(0.333, abs=0.001)
@pytest.mark.parametrize("a,b,expected", [
(2, 3, 5),
(-1, 1, 0),
(0, 0, 0),
(100, -50, 50),
])
def test_add_cases(a, b, expected):
assert add(a, b) == expected
import pytest
import json
import tempfile
import os
@pytest.fixture
def sample_users():
"""Provide test user data."""
return [
{"id": 1, "name": "Alice", "email": "[email protected]"},
{"id": 2, "name": "Bob", "email": "[email protected]"},
]
@pytest.fixture
def temp_db(tmp_path):
"""Provide a temporary SQLite database."""
import sqlite3
db_path = tmp_path / "test.db"
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)")
conn.commit()
yield conn
conn.close()
def test_insert_users(temp_db, sample_users):
for user in sample_users:
temp_db.execute("INSERT INTO users VALUES (?, ?, ?)",
(user["id"], user["name"], user["email"]))
temp_db.commit()
count = temp_db.execute("SELECT COUNT(*) FROM users").fetchone()[0]
assert count == 2
# Fixture with cleanup
@pytest.fixture
def temp_config_file():
path = tempfile.mktemp(suffix=".json")
with open(path, "w") as f:
json.dump({"key": "value"}, f)
yield path
os.unlink(path)
from unittest.mock import patch, MagicMock, AsyncMock
# Mock a function
@patch('mymodule.requests.get')
def test_fetch_data(mock_get):
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"data": "test"}
result = fetch_data("https://api.example.com")
assert result == {"data": "test"}
mock_get.assert_called_once_with("https://api.example.com")
# Mock async
@patch('mymodule.aiohttp.ClientSession.get', new_callable=AsyncMock)
async def test_async_fetch(mock_get):
mock_get.return_value.__aenter__.return_value.json = AsyncMock(return_value={"ok": True})
result = await async_fetch("/endpoint")
assert result["ok"] is True
# Context manager mock
def test_file_reader():
with patch("builtins.open", MagicMock(return_value=MagicMock(
read=MagicMock(return_value='{"key": "val"}'),
__enter__=MagicMock(return_value=MagicMock(read=MagicMock(return_value='{"key": "val"}'))),
__exit__=MagicMock(return_value=False),
))):
result = read_config("fake.json")
assert result["key"] == "val"
# Run with coverage
pytest --cov=mypackage --cov-report=term-missing
# HTML report
pytest --cov=mypackage --cov-report=html
# Open htmlcov/index.html
# Fail if coverage below threshold
pytest --cov=mypackage --cov-fail-under=80
// math.go
package math
import "errors"
func Add(a, b int) int { return a + b }
func Divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
// math_test.go
package math
import (
"testing"
"math"
)
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive", 2, 3, 5},
{"negative", -1, 1, 0},
{"zeros", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.expected {
t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.expected)
}
})
}
}
...安装 Test Patterns 后,可以对 AI 说这些话来触发它
Help me get started with Test Patterns
Explains what Test Patterns does, walks through the setup, and runs a quick demo based on your current project
Use Test Patterns to write and run tests across languages and frameworks
Invokes Test Patterns with the right parameters and returns the result directly in the conversation
What can I do with Test Patterns in my developer & devops workflow?
Lists the top use cases for Test Patterns, with example commands for each scenario
将技能文件夹放到 ~/.claude/skills/test-patterns/ 目录(个人级,所有项目可用),或 .claude/skills/test-patterns/(项目级)。重启 AI 客户端后,用 /test-patterns 主动调用,或让 AI 根据上下文自动发现并使用。
Test Patterns 支持 Claude、Cursor、OpenClaw,可与这些 AI 平台无缝集成,扩展其能力。
Test Patterns 可免费安装使用。请查阅仓库了解许可证信息。
Write and run tests across languages and frameworks. Use when setting up test suites, writing unit/integration/E2E tests, measuring coverage, mocking dependencies, or debugging test failures. Covers Node.js (Jest/Vitest), Python (pytest), Go, Rust, and Bash.
Test Patterns 属于「Developer & DevOps」分类,该分类的技能帮助 AI 智能体在此领域执行专业任务。
Automate my developer & devops tasks using Test Patterns
Identifies repetitive steps in your workflow and sets up Test Patterns to handle them automatically