pytest-async-testing
Provides Complete patterns for testing async Python code with pytest: pytest-asyncio configuration, AsyncMock usage, async fixtures, testing FastAPI with AsyncClient, testing Kafka async producers/consumers, event loop and cleanup patterns. Use when: Testing async functions, async use cases, FastAPI endpoints, async database operations, Kafka async clients, or any async/await code patterns.
What this skill does
# Pytest Async Testing
## Purpose
Async testing requires special handling of event loops, context managers, and async fixtures. This skill provides production-ready patterns for testing all async scenarios in modern Python.
## Quick Start
Configure pytest for auto-detection of async tests:
```toml
# pyproject.toml
[tool.pytest.ini_options]
asyncio_mode = "auto" # Auto-detect async tests
asyncio_default_fixture_loop_scope = "function" # Max isolation per test
```
Then write async tests naturally:
```python
@pytest.mark.asyncio
async def test_async_function() -> None:
"""Async test with full type safety."""
result = await fetch_data()
assert result is not None
# With pytest-asyncio auto-mode, @pytest.mark.asyncio is optional:
async def test_auto_detected() -> None:
"""Async test without decorator."""
result = await fetch_data()
assert result is not None
```
## Instructions
### Step 1: Configure pytest-asyncio in pyproject.toml
```toml
[tool.pytest.ini_options]
# Auto-detect async tests (don't require @pytest.mark.asyncio)
asyncio_mode = "auto"
# Function scope: New event loop per test (maximum isolation)
asyncio_default_fixture_loop_scope = "function"
# Alternative scopes for performance:
# asyncio_default_fixture_loop_scope = "module" # Shared per module
# asyncio_default_fixture_loop_scope = "session" # Single for all tests
```
### Step 2: Create Async Fixtures
```python
from typing import AsyncGenerator
import pytest
# ✅ GOOD: Async fixture with proper type hints
@pytest.fixture
async def async_client() -> AsyncGenerator[AsyncClient, None]:
"""Provide async HTTP client."""
async with AsyncClient(app=app, base_url="http://test") as client:
yield client
# Cleanup happens after yield
# ✅ GOOD: Async database session
@pytest.fixture
async def db_session() -> AsyncGenerator[AsyncSession, None]:
"""Provide async database session."""
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session:
yield session
await session.rollback() # Cleanup
```
### Step 3: Use AsyncMock for Mocking Async Methods
```python
from unittest.mock import AsyncMock
# ✅ GOOD: AsyncMock for single async method
@pytest.fixture
def mock_gateway() -> AsyncMock:
"""Mock async gateway."""
mock = AsyncMock()
mock.fetch_orders.return_value = [order1, order2]
mock.close.return_value = None
return mock
# ✅ GOOD: AsyncMock for async generator
@pytest.fixture
def mock_async_generator() -> AsyncMock:
"""Mock async generator."""
mock = AsyncMock()
async def fake_generator():
yield item1
yield item2
yield item3
mock.stream.return_value = fake_generator()
return mock
```
### Step 4: Test Async Use Cases
```python
@pytest.mark.asyncio
async def test_extract_orders_use_case(
mock_gateway: AsyncMock,
mock_publisher: AsyncMock,
) -> None:
"""Test async use case with async mocks."""
use_case = ExtractOrdersUseCase(
gateway=mock_gateway,
publisher=mock_publisher
)
result = await use_case.execute()
assert result.orders_count == 2
# Verify async methods were awaited
mock_gateway.fetch_orders.assert_awaited_once()
assert mock_publisher.publish_order.await_count == 2
```
### Step 5: Test FastAPI Endpoints with AsyncClient
```python
from httpx import AsyncClient, ASGITransport
@pytest.fixture
async def test_client() -> AsyncGenerator[AsyncClient, None]:
"""Async HTTP client for FastAPI testing."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
@pytest.mark.asyncio
async def test_get_endpoint(test_client: AsyncClient) -> None:
"""Test FastAPI GET endpoint."""
response = await test_client.get("/top-products?count=10")
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert len(data) <= 10
@pytest.mark.asyncio
async def test_post_endpoint(test_client: AsyncClient) -> None:
"""Test FastAPI POST endpoint."""
payload = {"name": "Test", "price": 99.99}
response = await test_client.post("/products", json=payload)
assert response.status_code == 201
data = response.json()
assert data["id"] is not None
```
### Step 6: Test Async Context Managers
```python
from contextlib import asynccontextmanager
@asynccontextmanager
async def database_transaction():
"""Async context manager for transactions."""
session = Session()
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
@pytest.mark.asyncio
async def test_transaction_success() -> None:
"""Test successful transaction."""
async with database_transaction() as session:
await session.execute("INSERT INTO orders VALUES (1, 'test')")
# Verify commit was called
@pytest.mark.asyncio
async def test_transaction_rollback() -> None:
"""Test transaction rollback on error."""
with pytest.raises(ValueError):
async with database_transaction() as session:
await session.execute("INSERT INTO orders VALUES (1, 'test')")
raise ValueError("Simulated error")
# Verify rollback was called
```
### Step 7: Test Async Generators
```python
from typing import AsyncIterator
async def fetch_orders_stream() -> AsyncIterator[dict]:
"""Async generator yielding orders."""
for i in range(10):
yield {"id": str(i), "total": 100.0 * i}
@pytest.mark.asyncio
async def test_async_generator() -> None:
"""Test async generator consumption."""
orders = []
async for order in fetch_orders_stream():
orders.append(order)
assert len(orders) == 10
assert orders[0]["id"] == "0"
assert orders[9]["total"] == 900.0
@pytest.mark.asyncio
async def test_async_generator_with_early_termination() -> None:
"""Test breaking out of async generator."""
orders = []
async for order in fetch_orders_stream():
orders.append(order)
if len(orders) >= 5:
break
assert len(orders) == 5
```
### Step 8: Test Kafka Async Clients
```python
from unittest.mock import AsyncMock
@pytest.fixture
def mock_kafka_producer() -> AsyncMock:
"""Mock confluent-kafka async producer."""
mock = AsyncMock()
mock.produce.return_value = None
mock.flush.return_value = 0 # All delivered
mock.close.return_value = None
return mock
@pytest.fixture
def mock_kafka_consumer() -> AsyncMock:
"""Mock Kafka consumer with async iteration."""
mock = AsyncMock()
async def fake_consume():
yield {"key": "order_1", "value": {"id": "1"}}
yield {"key": "order_2", "value": {"id": "2"}}
mock.consume.return_value = fake_consume()
return mock
@pytest.mark.asyncio
async def test_kafka_producer_integration(
mock_kafka_producer: AsyncMock
) -> None:
"""Test async Kafka producer."""
producer = KafkaProducerAdapter(mock_kafka_producer)
await producer.publish("test_topic", "key", {"data": "value"})
mock_kafka_producer.produce.assert_called_once()
mock_kafka_producer.flush.assert_called_once()
@pytest.mark.asyncio
async def test_kafka_consumer_integration(
mock_kafka_consumer: AsyncMock
) -> None:
"""Test async Kafka consumer."""
consumer = KafkaConsumerAdapter(mock_kafka_consumer)
messages = []
async for message in consumer.consume():
messages.append(message)
assert len(messages) == 2
```
### Step 9: Handle Event Loop Cleanup
```python
import asyncio
@pytest.fixture
async def cleanup_tasks():
"""Ensure all async tasks are cleaned up."""
yield
# Cancel any pending tasks
pending = asyncio.all_tasks()
for task in peRelated in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.