agentleFS
kiko197kiko197/skill/unit-testing/SKILL.md

unit-testing

Skill0 starsChanged 3 months agoMITView on GitHub

为代码生成高质量、可维护的单元测试。当需要为函数/模块编写单测、隔离依赖(mock/stub)、覆盖关键分支与边界条件、按项目既有测试框架与约定对齐、并在写完运行验证时使用。

20 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use database-testing, contract-testing and 3 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • petrkindlmannSkill

    database-testing

    Validate database integrity, test migrations forward and backward, verify schema constraints, manage seed data, detect migration drift, and identify query performance issues. Covers PostgreSQL, MySQL, MongoDB with Prisma, TypeORM, Drizzle, and SQLAlchemy, plus Testcontainers test databases. Use when: "database test," "migration test," "migration rollback," "rollback test," "data integrity," "SQL test," "schema validation," "seed data," "query performance," "Testcontainers." Not for: synthetic data generation/masking at scale — use test-data-management; Docker/IaC test-environment provisioning — use test-environments; SQL injection — use security-testing. Related: test-data-management, test-environments, security-testing, ci-cd-integration.

    Also in 3 projects that use this

  • petrkindlmannSkill

    contract-testing

    Implement consumer-driven contract testing with Pact-JS (v16). Covers consumer test writing, broker-driven provider verification, Pact Broker setup, can-i-deploy as a deployment gate, webhook-triggered verification, pending pacts, and schema-first vs consumer-first approaches (OpenAPI/Ajv, Schemathesis). Use when: "contract test," "Pact," "consumer-driven," "API contract," "provider verification," "can-i-deploy." Not for: stubbing or mocking a dependency to isolate a test — use service-virtualization; general REST/GraphQL endpoint assertions against your own API — use api-testing. Related: api-testing, service-virtualization, ci-cd-integration, test-environments.

    Also in 3 projects that use this

  • jarosser06Skill

    test-strategy

    Plans pytest test strategies for Drift's 90%+ coverage requirement using coverage analysis, edge case identification, and test suite design. Use when determining what tests to write, analyzing coverage gaps, or planning test strategies.

    Also in 4 projects that use this

  • etewiahSkill

    e2e-testing

    Playwright E2E testing and Lighthouse performance auditing. Use when setting up E2E tests, running Playwright tests, performing Lighthouse audits, or debugging E2E test failures.

    Also in 4 projects that use this

  • Microsoft AzureSkill

    release-readiness

    Complete ODIN release documentation and validation checks before declaring user-visible work complete, preparing a release, or opening the Release-to-main pull request.

    Also in 3 projects that use this

The file

Files
SKILL.md

单元测试

为单个函数/模块编写小型、快速、隔离、可靠的测试,并保证与项目现有技术栈一致。

何时使用

  • 新功能要求测试覆盖。
  • 修复 bug 时先补回归测试(防复发)。
  • 重构高风险逻辑前建立“安全网”。

原则

  • 一个用例测一个行为,用清晰的测试名描述“被测对象 → 条件 → 期望结果”。
  • 隔离外部依赖:只测当前单元,网络/数据库/文件用 mock、stub、fakes。
  • 只断言契约/行为,不绑定实现细节,减少脆弱测试。
  • 3A 结构:Arrange(准备)→ Act(执行)→ Assert(断言)。

流程

1. 对齐项目既有约定

动手前先看已有测试:

  • 用什么框架(JUnit/pytest/Jest/go test/CppUTest…)与断言库。
  • 测试文件放哪、如何命名。
  • 是否用 TDD 风格还是“实现后补测试”风格。
  • 如何运行单个测试、如何跑全量。

2. 列出待覆盖的用例

对照被测代码的分支结构,覆盖:

  • 正常路径(happy path)。
  • 各边界值 / 极端值(空、0、负数、最大值、空串、null、临界索引)。
  • 错误 / 异常路径与错误信息。
  • 状态转换、并发/重入(如适用)。

3. 编写测试(示例:Python + pytest)

def add(a, b):
    """返回 a+b;任意参数为 None 时抛 ValueError。"""
    if a is None or b is None:
        raise ValueError("参数不能为 None")
    return a + b

def test_add_normal():
    assert add(2, 3) == 5            # happy path

def test_add_zero():
    assert add(0, 0) == 0            # 边界

def test_add_negative():
    assert add(-1, 1) == 0

def test_add_none_raises():
    with pytest.raises(ValueError):
        add(None, 3)                 # 异常路径

4. 隔离外部依赖(mock)

只对“被测单元绕过不掉的边界”做 mock,不要过度 mock。

from unittest.mock import patch

@patch("module.send_email")
def test_register_sends_mail(mock_send):
    register("a@b.com")
    mock_send.assert_called_once_with("a@b.com", ...)

5. 运行并验证(必须执行)

# 以 pytest 为例:先跑新写的,再跑全量
pytest path/to/test_file.py -k added       # 只跑新增
pytest                                      # 全量回归
  • 确认:新测试先失败后通过(证明测试真的在测逻辑),再确认全量无回归。
  • 若项目语言/框架不同,改用对应的运行命令(mvn test / go test / npm test…)。

完成标准

  • 用与项目一致的框架与命名约定。
  • 覆盖关键分支与边界/异常路径。
  • 测试可脱离外部环境运行(mock 掉网络/DB)。
  • 新测试运行通过,全量无回归。
  • 测试名能描述场景,断言不依赖实现细节。

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.