ln-743-test-infrastructure
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseln-743-test-infrastructure
ln-743-test-infrastructure
Type: L3 Worker
Category: 7XX Project Bootstrap
Parent: ln-740-quality-setup
Sets up testing frameworks, coverage tools, and sample tests for projects.
类型: L3 Worker
分类: 7XX 项目初始化
父项: ln-740-quality-setup
为项目搭建测试框架、覆盖率工具和示例测试。
Purpose & Scope
目的与范围
Does:
- Detects project stack to choose appropriate test framework
- Creates test configuration files
- Sets up coverage reporting with thresholds
- Creates sample tests demonstrating patterns
- Verifies test suite runs successfully
Does NOT:
- Configure linters (ln-741 does this)
- Set up pre-commit hooks (ln-742 does this)
- Write actual application tests (developers do this)
负责内容:
- 检测项目技术栈以选择合适的测试框架
- 创建测试配置文件
- 配置带阈值的覆盖率报告
- 创建展示测试模式的示例测试
- 验证测试套件可成功运行
不负责内容:
- 配置代码检查工具(由ln-741负责)
- 配置提交前钩子(由ln-742负责)
- 编写实际的应用测试(由开发人员负责)
Supported Stacks
支持的技术栈
| Technology | Test Framework | Coverage Tool | Config File |
|---|---|---|---|
| TypeScript/React | Vitest | v8/Istanbul | |
| .NET | xUnit | Coverlet | |
| Python | pytest | pytest-cov | |
| 技术 | 测试框架 | 覆盖率工具 | 配置文件 |
|---|---|---|---|
| TypeScript/React | Vitest | v8/Istanbul | |
| .NET | xUnit | Coverlet | |
| Python | pytest | pytest-cov | |
Phase 1: Check Existing Tests
阶段1:检查现有测试
Before creating test infrastructure, check what exists.
Files to Check:
| Stack | Test Indicators |
|---|---|
| TypeScript | |
| .NET | |
| Python | |
Decision Logic:
- If complete test setup exists: SKIP (inform user)
- If partial setup: ASK to extend or replace
- If no tests: CREATE from templates
在创建测试基础设施之前,先检查已有的内容。
需要检查的文件:
| 技术栈 | 测试标识 |
|---|---|
| TypeScript | |
| .NET | |
| Python | |
决策逻辑:
- 若存在完整的测试配置:跳过(通知用户)
- 若存在部分配置:询问用户是扩展还是替换
- 若无任何测试:基于模板创建
Phase 2: Create Test Configuration
阶段2:创建测试配置
TypeScript/React (Vitest)
TypeScript/React (Vitest)
Create :
vitest.config.ts- Use v8 coverage provider (faster than Istanbul)
- Configure jsdom environment for React
- Set coverage thresholds (80% minimum)
- Create setup file for testing-library
Dependencies:
npm install -D vitest @vitest/coverage-v8 @testing-library/react @testing-library/jest-dom jsdom创建 :
vitest.config.ts- 使用v8覆盖率提供器(比Istanbul更快)
- 为React配置jsdom环境
- 设置覆盖率阈值(最低80%)
- 创建用于testing-library的设置文件
依赖项:
npm install -D vitest @vitest/coverage-v8 @testing-library/react @testing-library/jest-dom jsdom.NET (xUnit)
.NET (xUnit)
Create test project:
bash
dotnet new xunit -n {Project}.Tests
dotnet sln add tests/{Project}.TestsDependencies (in .csproj):
- Microsoft.NET.Test.Sdk
- xunit
- xunit.runner.visualstudio
- Moq
- FluentAssertions
- coverlet.collector
创建测试项目:
bash
dotnet new xunit -n {Project}.Tests
dotnet sln add tests/{Project}.Tests依赖项(在.csproj中):
- Microsoft.NET.Test.Sdk
- xunit
- xunit.runner.visualstudio
- Moq
- FluentAssertions
- coverlet.collector
Python (pytest)
Python (pytest)
Add to or create :
pyproject.tomlpytest.ini- Configure test discovery paths
- Set coverage thresholds (80% minimum)
- Configure coverage reporting
Dependencies:
pip install pytest pytest-cov pytest-asyncio添加至 或创建 :
pyproject.tomlpytest.ini- 配置测试发现路径
- 设置覆盖率阈值(最低80%)
- 配置覆盖率报告
依赖项:
pip install pytest pytest-cov pytest-asyncioOR with uv:
或使用uv:
uv add --dev pytest pytest-cov pytest-asyncio
---uv add --dev pytest pytest-cov pytest-asyncio
---Phase 3: Create Test Directory Structure
阶段3:创建测试目录结构
TypeScript
TypeScript
src/
├── components/
│ ├── Button.tsx
│ └── Button.test.tsx # Co-located tests
├── test/
│ └── setup.ts # Test setup filesrc/
├── components/
│ ├── Button.tsx
│ └── Button.test.tsx # 与源码同目录的测试文件
├── test/
│ └── setup.ts # 测试设置文件.NET
.NET
tests/
├── {Project}.Tests/
│ ├── Controllers/
│ │ └── SampleControllerTests.cs
│ ├── Services/
│ └── {Project}.Tests.csproj
└── {Project}.IntegrationTests/ # Optionaltests/
├── {Project}.Tests/
│ ├── Controllers/
│ │ └── SampleControllerTests.cs
│ ├── Services/
│ └── {Project}.Tests.csproj
└── {Project}.IntegrationTests/ # 可选Python
Python
tests/
├── __init__.py
├── conftest.py # Fixtures
├── unit/
│ └── test_sample.py
└── integration/ # Optionaltests/
├── __init__.py
├── conftest.py # 夹具文件
├── unit/
│ └── test_sample.py
└── integration/ # 可选Phase 4: Create Sample Tests
阶段4:创建示例测试
Create one sample test per stack demonstrating:
- AAA pattern (Arrange-Act-Assert)
- Test naming conventions
- Basic assertions
- Framework-specific patterns
为每个技术栈创建一个示例测试,展示:
- AAA模式(准备-执行-断言)
- 测试命名规范
- 基础断言
- 框架特定模式
TypeScript Sample Test
TypeScript示例测试
Shows:
- render() from testing-library
- screen queries
- Jest-dom matchers
展示:
- testing-library的render()方法
- screen查询
- Jest-dom匹配器
.NET Sample Test
.NET示例测试
Shows:
- [Fact] attribute
- Moq for mocking
- FluentAssertions syntax
展示:
- [Fact]特性
- 使用Moq进行模拟
- FluentAssertions语法
Python Sample Test
Python示例测试
Shows:
- pytest fixtures
- assert statements
- parametrized tests (optional)
展示:
- pytest夹具
- assert语句
- 参数化测试(可选)
Phase 5: Verify Test Run
阶段5:验证测试运行
After setup, verify tests work.
TypeScript:
bash
npm test
npm run test:coverageExpected: Sample test passes, coverage report generated
.NET:
bash
dotnet test
dotnet test --collect:"XPlat Code Coverage"Expected: Sample test passes, coverage collected
Python:
bash
pytest
pytest --cov=src --cov-report=term-missingExpected: Sample test passes, coverage report shown
On Failure: Check test configuration, dependencies, verify sample test syntax.
搭建完成后,验证测试可正常运行。
TypeScript:
bash
npm test
npm run test:coverage预期结果:示例测试通过,生成覆盖率报告
.NET:
bash
dotnet test
dotnet test --collect:"XPlat Code Coverage"预期结果:示例测试通过,收集覆盖率数据
Python:
bash
pytest
pytest --cov=src --cov-report=term-missing预期结果:示例测试通过,显示覆盖率报告
若失败: 检查测试配置、依赖项,验证示例测试语法。
Coverage Requirements
覆盖率要求
| Metric | Minimum | Target |
|---|---|---|
| Lines | 70% | 80% |
| Branches | 70% | 80% |
| Functions | 70% | 80% |
| Statements | 70% | 80% |
Configure CI to fail if coverage drops below thresholds.
| 指标 | 最低要求 | 目标值 |
|---|---|---|
| 代码行 | 70% | 80% |
| 分支 | 70% | 80% |
| 函数 | 70% | 80% |
| 语句 | 70% | 80% |
配置CI,若覆盖率低于阈值则构建失败。
Critical Rules
关键规则
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).
规则1: 必须配置覆盖率阈值,无例外。
规则2: 示例测试必须可通过,不得创建无法运行的示例。
规则3: 所有示例测试必须使用AAA模式(准备-执行-断言)。
规则4: 单元测试需与源码同目录存放(TypeScript)或使用tests/目录(.NET、Python)。
Definition of Done
完成标准
- Test framework installed and configured
- Coverage tool configured with 80% threshold
- Test directory structure created
- Sample test created and passing
- /
npm test/dotnet testruns successfullypytest - Coverage report generates
- User informed of:
- How to run tests
- Where to add new tests
- Coverage requirements
- 测试框架已安装并配置
- 覆盖率工具已配置,阈值设为80%
- 测试目录结构已创建
- 示例测试已创建且可通过
- /
npm test/dotnet test可成功运行pytest - 覆盖率报告可生成
- 已告知用户:
- 如何运行测试
- 新增测试的存放位置
- 覆盖率要求
Reference Files
参考文件
| 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 | pytest test example |
| testing_guide.md | Testing best practices |
| 文件 | 用途 |
|---|---|
| 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测试示例 |
| testing_guide.md | 测试最佳实践指南 |
Error Handling
错误处理
| Error | Cause | Resolution |
|---|---|---|
| Vitest not found | Not installed | |
| jsdom errors | Missing dependency | |
| xUnit discovery fails | SDK version mismatch | Update Microsoft.NET.Test.Sdk |
| pytest not found | Not in PATH | |
| Coverage 0% | Wrong source path | Check coverage.include config |
Version: 2.0.0
Last Updated: 2026-01-10
| 错误 | 原因 | 解决方法 |
|---|---|---|
| Vitest未找到 | 未安装 | 执行 |
| jsdom错误 | 缺少依赖项 | 执行 |
| xUnit发现测试失败 | SDK版本不匹配 | 更新Microsoft.NET.Test.Sdk |
| pytest未找到 | 不在PATH中 | 执行 |
| 覆盖率为0% | 源码路径错误 | 检查coverage.include配置 |
版本: 2.0.0
最后更新: 2026-01-10