ln-743-test-infrastructure by levnikolaevich/claude-code-skills
npx skills add https://github.com/levnikolaevich/claude-code-skills --skill ln-743-test-infrastructure路径说明: 文件路径 (
shared/、references/、../ln-*) 是相对于技能仓库根目录的。如果在当前工作目录未找到,请定位此 SKILL.md 文件所在的目录,然后向上返回一级以找到仓库根目录。如果缺少shared/目录,请通过 WebFetch 从https://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/skills/{path}获取文件。
类型: L3 工作者 类别: 7XX 项目引导 父级: ln-740-质量设置
为项目设置测试框架、覆盖率工具和示例测试。
负责:
不负责:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 技术 | 测试框架 | 覆盖率工具 | 配置文件 |
|---|---|---|---|
| TypeScript/React | Vitest | v8/Istanbul | vitest.config.ts |
| .NET | xUnit | Coverlet | *.Tests.csproj |
| Python | pytest | pytest-cov | pytest.ini 或 pyproject.toml |
在创建测试基础设施之前,先检查现有内容。
需要检查的文件:
| 技术栈 | 测试标识 |
|---|---|
| TypeScript | vitest.config.*、jest.config.*、*.test.ts、*.spec.ts |
| .NET | *.Tests.csproj、*.IntegrationTests.csproj |
| Python | pytest.ini、conftest.py、tests/、test_*.py |
决策逻辑:
创建 vitest.config.ts:
依赖项:
npm install -D vitest @vitest/coverage-v8 @testing-library/react @testing-library/jest-dom jsdom
创建测试项目:
dotnet new xunit -n {Project}.Tests
dotnet sln add tests/{Project}.Tests
依赖项(在 .csproj 中):
添加到 pyproject.toml 或创建 pytest.ini:
依赖项:
pip install pytest pytest-cov pytest-asyncio
# 或者使用 uv:
uv add --dev pytest pytest-cov pytest-asyncio
src/
├── components/
│ ├── Button.tsx
│ └── Button.test.tsx # 同位置测试
├── test/
│ └── setup.ts # 测试设置文件
tests/
├── {Project}.Tests/
│ ├── Controllers/
│ │ └── SampleControllerTests.cs
│ ├── Services/
│ └── {Project}.Tests.csproj
└── {Project}.IntegrationTests/ # 可选
tests/
├── conftest.py # 共享夹具(来自 conftest_template.py)
├── unit/
│ └── test_sample.py
└── integration/ # 可选
对于 FastAPI/异步项目,从 conftest_template.py 生成 conftest.py,包含共享的异步 HTTP 客户端夹具。根据项目调整应用的导入路径。
为每个技术栈创建一个示例测试,演示:
展示:
展示:
展示:
设置完成后,验证测试是否正常工作。
TypeScript:
npm test
npm run test:coverage
预期结果:示例测试通过,生成覆盖率报告
.NET:
dotnet test
dotnet test --collect:"XPlat Code Coverage"
预期结果:示例测试通过,收集覆盖率数据
Python:
pytest
pytest --cov=src --cov-report=term-missing
预期结果:示例测试通过,显示覆盖率报告
如果失败: 检查测试配置、依赖项,验证示例测试语法。
| 指标 | 最低要求 | 目标要求 |
|---|---|---|
| 行覆盖率 | 70% | 80% |
| 分支覆盖率 | 70% | 80% |
| 函数覆盖率 | 70% | 80% |
| 语句覆盖率 | 70% | 80% |
配置 CI,在覆盖率低于阈值时使构建失败。
规则 1: 必须配置覆盖率阈值。没有例外。
规则 2: 示例测试必须通过。不要创建损坏的示例。
规则 3: 在所有示例测试中使用 AAA 模式(Arrange-Act-Assert)。
规则 4: 单元测试与源代码放在一起(TypeScript)或使用 tests/ 目录(.NET,Python)。
npm test / dotnet test / pytest 成功运行| 文件 | 用途 |
|---|---|
| vitest_template.ts | Vitest 配置模板 |
| vitest_setup_template.ts | 测试设置文件 |
| react_test_template.tsx | React 组件测试 |
| xunit_csproj_template.xml | .NET 测试项目 |
| xunit_test_template.cs | xUnit 测试示例 |
| pytest_config_template.toml | pytest 配置 |
| pytest_test_template.py | pytest 测试示例 |
| conftest_template.py | 共享异步夹具(FastAPI) |
| testing_guide.md | 测试最佳实践 |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 未找到 Vitest | 未安装 | npm install -D vitest |
| jsdom 错误 | 缺少依赖项 | npm install -D jsdom |
| xUnit 发现失败 | SDK 版本不匹配 | 更新 Microsoft.NET.Test.Sdk |
| 未找到 pytest | 不在 PATH 中 | pip install pytest |
| 覆盖率 0% | 源路径错误 | 检查 coverage.include 配置 |
版本: 3.0.0 最后更新: 2026-03-18
每周安装数
150
仓库
GitHub 星标数
245
首次出现
2026年1月24日
安全审计
安装于
claude-code137
cursor134
gemini-cli133
codex133
opencode133
github-copilot128
Paths: File paths (
shared/,references/,../ln-*) are relative to skills repo root. If not found at CWD, locate this SKILL.md directory and go up one level for repo root. Ifshared/is missing, fetch files via WebFetch fromhttps://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/skills/{path}.
Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-740-quality-setup
Sets up testing frameworks, coverage tools, and sample tests for projects.
Does:
Does NOT:
| Technology | Test Framework | Coverage Tool | Config File |
|---|---|---|---|
| TypeScript/React | Vitest | v8/Istanbul | vitest.config.ts |
| .NET | xUnit | Coverlet | *.Tests.csproj |
| Python | pytest | pytest-cov | pytest.ini or pyproject.toml |
Before creating test infrastructure, check what exists.
Files to Check:
| Stack | Test Indicators |
|---|---|
| TypeScript | vitest.config.*, jest.config.*, *.test.ts, *.spec.ts |
| .NET | *.Tests.csproj, *.IntegrationTests.csproj |
| Python | pytest.ini, conftest.py, tests/, |
Decision Logic:
Create vitest.config.ts:
Dependencies:
npm install -D vitest @vitest/coverage-v8 @testing-library/react @testing-library/jest-dom jsdom
Create test project:
dotnet new xunit -n {Project}.Tests
dotnet sln add tests/{Project}.Tests
Dependencies (in .csproj):
Add to pyproject.toml or create pytest.ini:
Dependencies:
pip install pytest pytest-cov pytest-asyncio
# OR with uv:
uv add --dev pytest pytest-cov pytest-asyncio
src/
├── components/
│ ├── Button.tsx
│ └── Button.test.tsx # Co-located tests
├── test/
│ └── setup.ts # Test setup file
tests/
├── {Project}.Tests/
│ ├── Controllers/
│ │ └── SampleControllerTests.cs
│ ├── Services/
│ └── {Project}.Tests.csproj
└── {Project}.IntegrationTests/ # Optional
tests/
├── conftest.py # Shared fixtures (from conftest_template.py)
├── unit/
│ └── test_sample.py
└── integration/ # Optional
For FastAPI/async projects, generate conftest.py from conftest_template.py with shared async HTTP client fixture. Adapt the app import path to match the project.
Create one sample test per stack demonstrating:
Shows:
Shows:
Shows:
After setup, verify tests work.
TypeScript:
npm test
npm run test:coverage
Expected: Sample test passes, coverage report generated
.NET:
dotnet test
dotnet test --collect:"XPlat Code Coverage"
Expected: Sample test passes, coverage collected
Python:
pytest
pytest --cov=src --cov-report=term-missing
Expected: Sample test passes, coverage report shown
On Failure: Check test configuration, dependencies, verify sample test syntax.
| Metric | Minimum | Target |
|---|---|---|
| Lines | 70% | 80% |
| Branches | 70% | 80% |
| Functions | 70% | 80% |
| Statements | 70% | 80% |
Configure CI to fail if coverage drops below thresholds.
RULE 1: Coverage thresholds MUST be configured. No exceptions.
RULE 2: Sample tests MUST pass. Don't create broken examples.
RULE 3: Use AAA pattern (Arrange-Act-Assert) in all sample tests.
RULE 4: Co-locate unit tests with source (TypeScript) or use tests/ directory (.NET, Python).
npm test / dotnet test / pytest runs successfully| File | Purpose |
|---|---|
| vitest_template.ts | Vitest config template |
| vitest_setup_template.ts | Test setup file |
| react_test_template.tsx | React component test |
| xunit_csproj_template.xml | .NET test project |
| xunit_test_template.cs | xUnit test example |
| pytest_config_template.toml | pytest config |
| pytest_test_template.py |
| Error | Cause | Resolution |
|---|---|---|
| Vitest not found | Not installed | npm install -D vitest |
| jsdom errors | Missing dependency | npm install -D jsdom |
| xUnit discovery fails | SDK version mismatch | Update Microsoft.NET.Test.Sdk |
| pytest not found | Not in PATH | pip install pytest |
| Coverage 0% | Wrong source path | Check coverage.include config |
Version: 3.0.0 Last Updated: 2026-03-18
Weekly Installs
150
Repository
GitHub Stars
245
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code137
cursor134
gemini-cli133
codex133
opencode133
github-copilot128
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装
test_*.py| pytest test example |
| conftest_template.py | Shared async fixtures (FastAPI) |
| testing_guide.md | Testing best practices |