xiaohongshu
xiaohongshu
add-unit-tests
Guide for adding unit tests to AReaL. Use when user wants to add tests for new functionality or increase test coverage.
Full skill instructions
Add unit tests to AReaL following the project's testing conventions.
This skill is triggered when:
AReaL has two main test categories:
| Test Type | Purpose | Location Pattern | How It Runs |
|---|---|---|---|
| Unit Tests | Test individual functions/modules | areal/tests/test_<module>_<feature>.py | Directly via pytest |
| Distributed Tests | Test distributed/parallel behavior | areal/tests/torchrun/run_*.py | Via torchrun (called by pytest subprocess) |
Note: All tests are invoked via pytest. Distributed tests use torchrun but are
still called from pytest test files.
Create test file with naming convention: test_<module>_<feature>.py
import pytest
import torch
# Import the module to test
from areal.dataset.gsm8k import get_gsm8k_sft_dataset
from areal.tests.utils import get_dataset_path # Optional test utilities
# For mocking tokenizer: from unittest.mock import MagicMock
Follow Arrange-Act-Assert pattern:
def test_function_under_condition_returns_expected():
"""Test that function returns expected value under condition."""
# Arrange
input_data = 5
expected_output = 10
# Act
result = function_under_test(input_data)
# Assert
assert result == expected_output
Use appropriate pytest markers:
| Marker | When to Use |
|---|---|
@pytest.mark.slow | Test takes > 10 seconds (excluded from CI by default) |
@pytest.mark.ci | Slow test that must run in CI (use with @pytest.mark.slow) |
@pytest.mark.asyncio | Async test functions |
@pytest.mark.skipif(cond, reason=...) | Conditional skip |
@pytest.mark.parametrize(...) | Parameterized tests |
CI Test Strategy:
@pytest.mark.slow: Excluded from CI by default (CI runs pytest -m "not slow")@pytest.mark.slow + @pytest.mark.ci: Slow but must run in CI@pytest.mark.asyncio
async def test_async_function():
result = await async_function()
assert result == expected
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available")
def test_gpu_feature():
tensor = torch.tensor([1, 2, 3], device="cuda")
# ... assertions
@pytest.mark.parametrize("batch_size", [1, 4, 16])
def test_with_parameters(batch_size):
# Parameterized test
@pytest.mark.slow
def test_slow_function():
# Excluded from CI by default
@pytest.mark.slow
@pytest.mark.ci
def test_slow_but_required_in_ci():
# Slow but must run in CI
For unit tests that need distributed mocks:
import torch.distributed as dist
def test_distributed_function(monkeypatch):
monkeypatch.setattr(dist, "get_rank", lambda: 0)
monkeypatch.setattr(dist, "get_world_size", lambda: 2)
result = distributed_function()
assert result == expected
Always skip gracefully when GPU unavailable:
CUDA_AVAILABLE = torch.cuda.is_available()
@pytest.mark.skipif(not CUDA_AVAILABLE, reason="CUDA not available")
def test_gpu_function():
tensor = torch.tensor([1, 2, 3], device="cuda")
# ... assertions
torch.distributed.fake_pg for unit testsdist.get_rank() and dist.get_world_size() explicitlytorch.cuda.empty_cache() in fixturestorch.testing.assert_close() for tensor comparisonrtol/atol explicitly for numerical testsassert tensor.equal() — no useful error message| Test File | Description | Key Patterns |
|---|---|---|
areal/tests/test_utils.py | Utility function tests | Fixtures, parametrized tests |
areal/tests/test_examples.py | Integration tests with dataset loading | Dataset path resolution, success pattern matching |
areal/tests/test_fsdp_engine_nccl.py | Distributed tests | Torchrun integration |
test_*.py naming@pytest.mark.skipif for GPU teststorch.testing.assert_close() not
assert tensor.equal()torch.cuda.empty_cache()test_<what>_<condition>_<expected> patternThis skill complements other AReaL development skills:
/add-dataset: Add tests for new dataset loaders/add-workflow: Add tests for new workflows/add-reward: Add tests for new reward functionsplanner agent: Reference this skill when planning test implementation# Run specific test file
uv run pytest areal/tests/test_<name>.py
# Skip slow tests (CI default)
uv run pytest -m "not slow"
# Run with verbose output
uv run pytest -v
# Run distributed tests (requires torchrun)
# Note: Usually invoked via pytest test files
torchrun --nproc_per_node=2 areal/tests/torchrun/run_<test>.py
<!--
================================================================================
MAINTAINER GUIDE
================================================================================
Location: .claude/skills/add-unit-tests/SKILL.md
Invocation: /add-unit-tests
## Purpose
Step-by-step guide for adding unit tests to AReaL.
## How to Update
### When Testing Conventions Change
1. Update "Key Requirements" section based on `testing.md`
2. Update test examples to match new patterns
3. Update reference implementations
### When Test Types Need Update
1. Update "Understand Test Types" table (currently two main types)
2. Add new examples if needed
3. Update common mistakes
### Integration with Other Skills
Ensure references to other skills (`/add-dataset`, `/add-workflow`, `/add-reward`) remain accurate.
================================================================================
-->xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience