ln-743-test-infrastructure

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ln-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

支持的技术栈

TechnologyTest FrameworkCoverage ToolConfig File
TypeScript/ReactVitestv8/Istanbul
vitest.config.ts
.NETxUnitCoverlet
*.Tests.csproj
Pythonpytestpytest-cov
pytest.ini
or
pyproject.toml

技术测试框架覆盖率工具配置文件
TypeScript/ReactVitestv8/Istanbul
vitest.config.ts
.NETxUnitCoverlet
*.Tests.csproj
Pythonpytestpytest-cov
pytest.ini
pyproject.toml

Phase 1: Check Existing Tests

阶段1:检查现有测试

Before creating test infrastructure, check what exists.
Files to Check:
StackTest Indicators
TypeScript
vitest.config.*
,
jest.config.*
,
*.test.ts
,
*.spec.ts
.NET
*.Tests.csproj
,
*.IntegrationTests.csproj
Python
pytest.ini
,
conftest.py
,
tests/
,
test_*.py
Decision Logic:
  1. If complete test setup exists: SKIP (inform user)
  2. If partial setup: ASK to extend or replace
  3. If no tests: CREATE from templates

在创建测试基础设施之前,先检查已有的内容。
需要检查的文件:
技术栈测试标识
TypeScript
vitest.config.*
,
jest.config.*
,
*.test.ts
,
*.spec.ts
.NET
*.Tests.csproj
,
*.IntegrationTests.csproj
Python
pytest.ini
,
conftest.py
,
tests/
,
test_*.py
决策逻辑:
  1. 若存在完整的测试配置:跳过(通知用户)
  2. 若存在部分配置:询问用户是扩展还是替换
  3. 若无任何测试:基于模板创建

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}.Tests
Dependencies (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
pyproject.toml
or create
pytest.ini
:
  • Configure test discovery paths
  • Set coverage thresholds (80% minimum)
  • Configure coverage reporting
Dependencies:
pip install pytest pytest-cov pytest-asyncio
添加至
pyproject.toml
或创建
pytest.ini
:
  • 配置测试发现路径
  • 设置覆盖率阈值(最低80%)
  • 配置覆盖率报告
依赖项:
pip install pytest pytest-cov pytest-asyncio

OR 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 file
src/
├── components/
│   ├── Button.tsx
│   └── Button.test.tsx  # 与源码同目录的测试文件
├── test/
│   └── setup.ts         # 测试设置文件

.NET

.NET

tests/
├── {Project}.Tests/
│   ├── Controllers/
│   │   └── SampleControllerTests.cs
│   ├── Services/
│   └── {Project}.Tests.csproj
└── {Project}.IntegrationTests/  # Optional
tests/
├── {Project}.Tests/
│   ├── Controllers/
│   │   └── SampleControllerTests.cs
│   ├── Services/
│   └── {Project}.Tests.csproj
└── {Project}.IntegrationTests/  # 可选

Python

Python

tests/
├── __init__.py
├── conftest.py          # Fixtures
├── unit/
│   └── test_sample.py
└── integration/         # Optional

tests/
├── __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:coverage
Expected: 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-missing
Expected: 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

覆盖率要求

MetricMinimumTarget
Lines70%80%
Branches70%80%
Functions70%80%
Statements70%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 test
    /
    pytest
    runs successfully
  • 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

参考文件

FilePurpose
vitest_template.tsVitest config template
vitest_setup_template.tsTest setup file
react_test_template.tsxReact component test
xunit_csproj_template.xml.NET test project
xunit_test_template.csxUnit test example
pytest_config_template.tomlpytest config
pytest_test_template.pypytest test example
testing_guide.mdTesting best practices

文件用途
vitest_template.tsVitest配置模板
vitest_setup_template.ts测试设置文件
react_test_template.tsxReact组件测试模板
xunit_csproj_template.xml.NET测试项目模板
xunit_test_template.csxUnit测试示例
pytest_config_template.tomlpytest配置模板
pytest_test_template.pypytest测试示例
testing_guide.md测试最佳实践指南

Error Handling

错误处理

ErrorCauseResolution
Vitest not foundNot installed
npm install -D vitest
jsdom errorsMissing dependency
npm install -D jsdom
xUnit discovery failsSDK version mismatchUpdate Microsoft.NET.Test.Sdk
pytest not foundNot in PATH
pip install pytest
Coverage 0%Wrong source pathCheck coverage.include config

Version: 2.0.0 Last Updated: 2026-01-10
错误原因解决方法
Vitest未找到未安装执行
npm install -D vitest
jsdom错误缺少依赖项执行
npm install -D jsdom
xUnit发现测试失败SDK版本不匹配更新Microsoft.NET.Test.Sdk
pytest未找到不在PATH中执行
pip install pytest
覆盖率为0%源码路径错误检查coverage.include配置

版本: 2.0.0 最后更新: 2026-01-10