Command Palette

Search for a command to run...

Back to skills
skill № 09875

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…

FreeNo executable scripts
sean-gallowayby 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
Sign up freeI already have an account

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:

  1. Use CocoTB framework
  2. Import appropriate BFMs from bin/TBClasses/
  3. Target >95% functional coverage
  4. Document test methodology in file header
  5. Include waveform dumps for debugging

Test File Location:

  • val/common/test_{module}.py for rtl/common/
  • val/math/test_{module}.py for rtl/math/
  • val/amba/test_{module}.py for 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/):

  1. All CocoTB functions MUST be prefixed with cocotb_test_*

    • Prevents pytest from collecting them as test functions
    • Only pytest wrappers (test_*) are collected
  2. Each pytest wrapper calls ONE specific CocoTB function

    • Use testcase="cocotb_test_name" in run() call
    • Allows parameter sweeps at pytest level
  3. 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
Full SKILL.md on GitHub

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

Compare test-patterns with