Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

pytest

БесплатноБез исполняемых скриптовНе проверен

pytest Python testing framework. Covers fixtures, parametrize, and mocking. Use for Python testing. USE WHEN: user mentions "pytest", "python test"…

claude-dev-suiteот claude-dev-suite★ 51 звезда на GitHub

Об этом скилле

pytest Core Knowledge

Deep Knowledge: Use mcp__documentation__fetch_docs with technology: pytest for comprehensive documentation.

When NOT to Use This Skill

  • JavaScript/TypeScript Testing - Use vitest or jest for JS/TS projects
  • Java Testing - Use junit for Java/Spring projects
  • E2E Web Testing - Use playwright for browser automation
  • Load Testing - Use locust or pytest-benchmark for performance
  • API Contract Testing - Use dedicated tools like Pact

Basic Tests

def test_addition():
    assert 1 + 1 == 2

def test_list_contains():
    items = [1, 2, 3]
    assert 2 in items

def test_raises_exception():
    with pytest.raises(ValueError):
        int("not a number")

class TestUserService:
    def test_create_user(self):
        user = create_user(name="John")
        assert user.name == "John"

Fixtures

import pytest

@pytest.fixture
def user():
    return User(name="John", email="[email protected]")

@pytest.fixture
def db():
    connection = create_db_connection()
    yield connection
    connection.close()

def test_user_name(user):
    assert user.name == "John"

def test_user_in_db(db, user):
    db.save(user)
    assert db.find(user.id) is not None

Parametrize

@pytest.mark.parametrize("input,expected", [
    (1, 2),
    (2, 4),
    (3, 6),
])
def test_double(input, expected):
    assert double(input) == expected

@pytest.mark.parametrize("email,valid", [
    ("[email protected]", True),
    ("invalid", False),
    ("", False),
])
def test_validate_email(email, valid):
    assert validate_email(email) == valid

Mocking

from unittest.mock import Mock, patch

def test_with_mock():
    mock_api = Mock()
    mock_api.get_users.return_value = [{"name": "John"}]

    result = process_users(mock_api)

    mock_api.get_users.assert_called_once()

@patch('myapp.api.requests.get')
def test_api_call(mock_get):
    mock_get.return_value.json.return_value = {"data": []}

    result = fetch_data()

    assert result == {"data": []}

Async Tests

import pytest

@pytest.mark.asyncio
async def test_async_function():
    result = await async_fetch_data()
    assert result is not None

# With pytest-asyncio 0.23+
@pytest.mark.asyncio(loop_scope="module")
async def test_shared_loop():
    pass

Property-Based Testing (Hypothesis)

from hypothesis import given, strategies as st

@given(st.integers(), st.integers())
def test_addition_commutative(a: int, b: int):
    assert a + b == b + a

@given(st.lists(st.integers()))
def test_sort_idempotent(items: list[int]):
    assert sorted(sorted(items)) == sorted(items)

@given(st.text(min_size=1))
def test_string_nonempty(s: str):
    assert len(s) >= 1

# Custom strategies
@given(st.builds(User, name=st.text(min_size=1), age=st.integers(0, 150)))
def test_user_creation(user: User):
    assert user.name
    assert 0 <= user.age <= 150

# Stateful testing
from hypothesis.stateful import RuleBasedStateMachine, rule

class SetMachine(RuleBasedStateMachine):
    def __init__(self):
        self.model = set()
        self.impl = MySet()

    @rule(value=st.integers())
    def add(self, value):
        self.model.add(value)
        self.impl.add(value)
        assert self.impl.contains(value)

Configuration

# pytest.ini
[pytest]
testpaths = tests
python_files = test_*.py
python_functions = test_*
addopts = -v --cov=src --cov-report=html

Production Readiness

Test Organization

# tests/conftest.py - Shared fixtures
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

@pytest.fixture(scope="session")
def engine():
    """Database engine for all tests."""
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    yield engine
    engine.dispose()

@pytest.fixture(scope="function")
def db_session(engine):
    """Fresh database session for each test."""
    Session = sessionmaker(bind=engine)
    session = Session()
    yield session
    session.rollback()
    session.close()

@pytest.fixture
def client(db_session):
    """Test client with database session."""
    app.dependency_overrides[get_db] = lambda: db_session
    with TestClient(app) as client:
        yield client
    app.dependency_overrides.clear()

Coverage Configuration

# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
addopts = "-v --cov=src --cov-report=term-missing --cov-report=xml --cov-fail-under=80"
markers = [
    "slow: marks tests as slow",
    "integration: integration tests",
]

[tool.coverage.run]
source = ["src"]
omit = ["*/tests/*", "*/__init__.py"]

[tool.coverage.report]
exclude_lines = [
    "pragma: no cover",
    "if TYPE_CHECKING:",
    "raise NotImplementedError",
]

CI Configuration

# GitHub Actions
- name: Run tests
  run: |
    pytest --junitxml=test-results.xml --cov-report=
Полный SKILL.md на GitHub

Установить pytest в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Read
Grep
Glob
Write
Edit

Вложенные файлы

quick-ref/alembic-testing.mdquick-ref/basics.mdquick-ref/fixtures.mdquick-ref/grpc-testing.mdquick-ref/integration-patterns.mdquick-ref/pact-python.mdquick-ref/redis-kafka-testing.mdquick-ref/sqlalchemy-fixtures.mdquick-ref/testcontainers-python.md

FAQ

Что делает скилл pytest?

pytest Python testing framework. Covers fixtures, parametrize, and mocking. Use for Python testing. USE WHEN: user mentions "pytest", "python test", "fixture", asks about "@pytest.fixture", "@pytest.mark.parametrize", "conftest.py", "pytest coverage", "python unit test" DO NOT USE FOR: JavaScript/TypeScript - use `vitest` or `jest`; Java - use `junit`; E2E web tests - use `playwright`; Load testing - use locust

Как установить скилл pytest?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл pytest запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить pytest с