test-patterns
Test structure for this repo: Pattern A (val/) vs Pattern B (projects/components/), the mandatory cocotb_test_* prefix, pytest function naming, the three…
by sean-galloway★ 30 GitHub stars
Install test-patterns in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Test patterns
Moved out of the root CLAUDE.md, which loaded all of this into every session
whether or not a test was in scope. /GLOBAL_REQUIREMENTS.md remains the
enforcement authority and wins on conflict.
Writing Tests
Every RTL module requires a test!
# Run specific test
pytest val/{subsystem}/test_{module}.py -v
# Run all tests in subsystem
pytest val/{subsystem}/ -v
# Run with coverage
pytest val/{subsystem}/ --cov=rtl/{subsystem}/
Test Structure:
- Use CocoTB framework
- Import appropriate BFMs from
bin/TBClasses/ - Target >95% functional coverage
- Document test methodology in file header
- Include waveform dumps for debugging
Test File Location:
val/common/test_{module}.pyfor rtl/common/val/math/test_{module}.pyfor rtl/math/val/amba/test_{module}.pyfor rtl/amba/projects/components/{name}/dv/tests/for project-specific tests (RAPIDS, STREAM, bridge, ...)
CRITICAL: Test Structure Pattern
The repository uses TWO different test patterns depending on the location:
Pattern A: Direct CocoTB (val/common/, val/amba/ areas)
import cocotb
from cocotb_test.simulator import run
# CocoTB test function - direct @cocotb.test() decorator
@cocotb.test(timeout_time=3, timeout_unit="ms")
async def fifo_test(dut):
tb = FifoBufferTB(dut, dut.clk, dut.rst_n)
await tb.start_clock('clk', 10, 'ns')
# ... test logic
# Pytest wrapper function
@pytest.mark.parametrize("data_width, depth", params)
def test_fifo_buffer(request, data_width, depth):
# ... setup paths, filelist, parameters
run(
python_search=[tests_dir],
verilog_sources=verilog_sources,
toplevel=dut_name,
module=module, # Python module containing cocotb tests
# ... compilation args
)
Pattern B: CocoTB + Pytest Wrappers (projects/components/ areas)
HARD REQUIREMENT for projects/components/: MUST use Pattern B
import cocotb
from cocotb_test.simulator import run
# 1. CocoTB test functions - prefix with "cocotb_test_*" to prevent pytest collection
@cocotb.test(timeout_time=100, timeout_unit="ms")
async def cocotb_test_basic(dut): # ← "cocotb_test_*" prefix!
"""CocoTB test function - NOT collected by pytest"""
tb = SimpleSRAMTB(dut)
await tb.setup_clocks_and_reset()
# ... test logic
@cocotb.test(timeout_time=100, timeout_unit="ms")
async def cocotb_test_stress(dut): # ← "cocotb_test_*" prefix!
"""Another CocoTB test function"""
tb = SimpleSRAMTB(dut)
await tb.setup_clocks_and_reset()
# ... stress test logic
# 2. Pytest wrapper functions - call specific cocotb_test_* functions
@pytest.mark.parametrize("addr_width, data_width", params)
def test_basic(request, addr_width, data_width):
"""Pytest wrapper - calls cocotb_test_basic"""
module, repo_root, tests_dir, log_dir, rtl_dict = get_paths({
'rtl_stream_fub': '../../../../rtl/fub',
})
verilog_sources, includes = get_sources_from_filelist(
repo_root=repo_root,
filelist_path='projects/components/dma-ip/stream/rtl/filelists/fub/sram_controller.f'
)
run(
python_search=[tests_dir],
verilog_sources=verilog_sources,
includes=includes,
toplevel=dut_name,
module=module,
testcase="cocotb_test_basic", # ← Explicitly specify which cocotb function to run
parameters=rtl_parameters,
# ... compilation args
)
@pytest.mark.parametrize("addr_width, data_width", params)
def test_stress(request, addr_width, data_width):
"""Pytest wrapper - calls cocotb_test_stress"""
# ... same setup as above
run(
# ... same args except:
testcase="cocotb_test_stress", # ← Different cocotb function
# ...
)
Why Two Patterns?
| Aspect | Pattern A (val/) | Pattern B (projects/) |
|---|---|---|
| CocoTB prefix | No prefix needed | cocotb_test_* prefix REQUIRED |
| Pytest collection | Collects module | Collects only wrappers |
| Test selection | Runs all cocotb tests | Runs specific test via testcase= |
| Use case | Simple modules | Complex parameterized tests |
| Example | Counter, FIFO | SRAM, engines, integration |
Critical Rules for Pattern B (projects/components/):
All CocoTB functions MUST be prefixed with
cocotb_test_*- Prevents pytest from collecting them as test functions
- Only pytest wrappers (test_*) are collected
Each pytest wrapper calls ONE specific CocoTB function
- Use
testcase="cocotb_test_name"in run() call - Allows parameter sweeps at pytest level
- Use
Testbench classes MUST be in project area
projects/components/{name}/dv/tbclasses/(NOT framework!)- See "Organizational Requirements" section
When to Use Which Pattern:
- Use Pattern A: Simple modules in val/common, val/amba
- Use Pattern B: ALL tests in projects/components/
- Never mix patterns
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the test-patterns skill do?
Test structure for this repo: Pattern A (val/) vs Pattern B (projects/components/), the mandatory cocotb_test_* prefix, pytest function naming, the three required TB methods, and the gate/func/full level convention. Use before writing or changing any test.
How do I install the test-patterns skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the test-patterns skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
doc-coauthoring
AnthropicGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
academy-guide
AnthropicStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
internal-comms
AnthropicA set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
skill-development
AnthropicThis skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…