Loading...
Loading...
Compare original and translation side by side
| CRAP Score | Risk Level | Action Required |
|---|---|---|
| < 5 | Low | Well-tested, maintainable code |
| 5-30 | Medium | Acceptable but watch complexity |
| > 30 | High | Needs tests or refactoring |
| CRAP分数 | 风险等级 | 所需操作 |
|---|---|---|
| < 5 | 低 | 测试充分、可维护的代码 |
| 5-30 | 中 | 可接受,但需关注复杂度 |
| > 30 | 高 | 需要补充测试或重构 |
| Method | Complexity | Coverage | Calculation | CRAP |
|---|---|---|---|---|
| 1 | 0% | 1 x (1 - 0)^2 | 1 |
| 54 | 52% | 54 x (1 - 0.52)^2 | 12.4 |
| 20 | 0% | 20 x (1 - 0)^2 | 20 |
| 45 | 20% | 45 x (1 - 0.20)^2 | 28.8 |
| 80 | 10% | 80 x (1 - 0.10)^2 | 64.8 |
| 方法 | 圈复杂度 | 覆盖率 | 计算过程 | CRAP分数 |
|---|---|---|---|---|
| 1 | 0% | 1 × (1 - 0)² | 1 |
| 54 | 52% | 54 × (1 - 0.52)² | 12.4 |
| 20 | 0% | 20 × (1 - 0)² | 20 |
| 45 | 20% | 45 × (1 - 0.20)² | 28.8 |
| 80 | 10% | 80 × (1 - 0.10)² | 64.8 |
coverage.runsettings<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<!-- OpenCover format includes cyclomatic complexity for CRAP scores -->
<Format>cobertura,opencover</Format>
<!-- Exclude test and benchmark assemblies -->
<Exclude>[*.Tests]*,[*.Benchmark]*,[*.Migrations]*</Exclude>
<!-- Exclude generated code, obsolete members, and explicit exclusions -->
<ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute,ExcludeFromCodeCoverageAttribute</ExcludeByAttribute>
<!-- Exclude source-generated files, Blazor generated code, and migrations -->
<ExcludeByFile>**/obj/**/*,**/*.g.cs,**/*.designer.cs,**/*.razor.g.cs,**/*.razor.css.g.cs,**/Migrations/**/*</ExcludeByFile>
<!-- Exclude test projects -->
<IncludeTestAssembly>false</IncludeTestAssembly>
<!-- Optimization flags -->
<SingleHit>false</SingleHit>
<UseSourceLink>true</UseSourceLink>
<SkipAutoProps>true</SkipAutoProps>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>coverage.runsettings<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<!-- OpenCover format includes cyclomatic complexity for CRAP scores -->
<Format>cobertura,opencover</Format>
<!-- Exclude test and benchmark assemblies -->
<Exclude>[*.Tests]*,[*.Benchmark]*,[*.Migrations]*</Exclude>
<!-- Exclude generated code, obsolete members, and explicit exclusions -->
<ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute,ExcludeFromCodeCoverageAttribute</ExcludeByAttribute>
<!-- Exclude source-generated files, Blazor generated code, and migrations -->
<ExcludeByFile>**/obj/**/*,**/*.g.cs,**/*.designer.cs,**/*.razor.g.cs,**/*.razor.css.g.cs,**/Migrations/**/*</ExcludeByFile>
<!-- Exclude test projects -->
<IncludeTestAssembly>false</IncludeTestAssembly>
<!-- Optimization flags -->
<SingleHit>false</SingleHit>
<UseSourceLink>true</UseSourceLink>
<SkipAutoProps>true</SkipAutoProps>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>| Option | Purpose |
|---|---|
| Must include |
| Exclude test/benchmark assemblies by pattern |
| Skip generated, obsolete, and explicitly excluded code (includes |
| Skip source-generated files, Blazor components, and migrations |
| Don't count auto-properties as branches |
| 选项 | 用途 |
|---|---|
| 必须包含 |
| 通过模式排除测试/基准测试程序集 |
| 跳过生成代码、过时成员和显式排除的代码(包括 |
| 跳过源生成文件、Blazor组件和迁移文件 |
| 不将自动属性计入分支 |
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-reportgenerator-globaltool": {
"version": "5.4.5",
"commands": ["reportgenerator"],
"rollForward": false
}
}
}dotnet tool restore{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-reportgenerator-globaltool": {
"version": "5.4.5",
"commands": ["reportgenerator"],
"rollForward": false
}
}
}dotnet tool restoredotnet tool install --global dotnet-reportgenerator-globaltooldotnet tool install --global dotnet-reportgenerator-globaltoolundefinedundefinedundefinedundefineddotnet reportgenerator \
-reports:"TestResults/**/coverage.opencover.xml" \
-targetdir:"coverage" \
-reporttypes:"Html;TextSummary;MarkdownSummaryGithub"dotnet reportgenerator \
-reports:"TestResults/**/coverage.opencover.xml" \
-targetdir:"coverage" \
-reporttypes:"Html;TextSummary;MarkdownSummaryGithub"| Type | Description | Output |
|---|---|---|
| Full interactive report | |
| Plain text summary | |
| GitHub-compatible markdown | |
| SVG badges for README | |
| Merged Cobertura XML | |
| 类型 | 描述 | 输出位置 |
|---|---|---|
| 完整交互式报告 | |
| 纯文本摘要 | |
| 兼容GitHub的Markdown格式 | |
| 用于README的SVG徽章 | |
| 合并后的Cobertura XML文件 | |
Risk Hotspots
─────────────
Method Complexity Coverage Crap Score
──────────────────────────────────────────────────────────────────
DataImporter.ParseRecord() 54 52% 12.4
AuthService.ValidateToken() 32 0% 32.0 ← HIGH RISK
OrderProcessor.Calculate() 28 85% 1.3
UserService.CreateUser() 15 100% 0.0ValidateToken()ParseRecord()CreateUser()Calculate()Risk Hotspots
─────────────
Method Complexity Coverage Crap Score
──────────────────────────────────────────────────────────────────
DataImporter.ParseRecord() 54 52% 12.4
AuthService.ValidateToken() 32 0% 32.0 ← HIGH RISK
OrderProcessor.Calculate() 28 85% 1.3
UserService.CreateUser() 15 100% 0.0ValidateToken()ParseRecord()CreateUser()Calculate()| Coverage Type | Target | Action |
|---|---|---|
| Line Coverage | > 80% | Good for most projects |
| Branch Coverage | > 60% | Catches conditional logic |
| CRAP Score | < 30 | Maximum for new code |
| 覆盖率类型 | 目标值 | 行动 |
|---|---|---|
| 行覆盖率 | > 80% | 适用于大多数项目 |
| 分支覆盖率 | > 60% | 覆盖条件逻辑测试 |
| CRAP分数 | < 30 | 新代码的最高允许值 |
coverage.props<Project>
<PropertyGroup>
<!-- Coverage thresholds for CI enforcement -->
<CoverageThresholdLine>80</CoverageThresholdLine>
<CoverageThresholdBranch>60</CoverageThresholdBranch>
</PropertyGroup>
</Project>coverage.props<Project>
<PropertyGroup>
<!-- Coverage thresholds for CI enforcement -->
<CoverageThresholdLine>80</CoverageThresholdLine>
<CoverageThresholdBranch>60</CoverageThresholdBranch>
</PropertyGroup>
</Project>name: Coverage
on:
pull_request:
branches: [main, dev]
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Restore tools
run: dotnet tool restore
- name: Run tests with coverage
run: |
dotnet test \
--settings coverage.runsettings \
--collect:"XPlat Code Coverage" \
--results-directory ./TestResults
- name: Generate report
run: |
dotnet reportgenerator \
-reports:"TestResults/**/coverage.opencover.xml" \
-targetdir:"coverage" \
-reporttypes:"Html;MarkdownSummaryGithub;Cobertura"
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/
- name: Add coverage to PR
uses: marocchino/sticky-pull-request-comment@v2
with:
path: coverage/SummaryGithub.mdname: Coverage
on:
pull_request:
branches: [main, dev]
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Restore tools
run: dotnet tool restore
- name: Run tests with coverage
run: |
dotnet test \
--settings coverage.runsettings \
--collect:"XPlat Code Coverage" \
--results-directory ./TestResults
- name: Generate report
run: |
dotnet reportgenerator \
-reports:"TestResults/**/coverage.opencover.xml" \
-targetdir:"coverage" \
-reporttypes:"Html;MarkdownSummaryGithub;Cobertura"
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/
- name: Add coverage to PR
uses: marocchino/sticky-pull-request-comment@v2
with:
path: coverage/SummaryGithub.md- task: DotNetCoreCLI@2
displayName: 'Run tests with coverage'
inputs:
command: 'test'
arguments: '--settings coverage.runsettings --collect:"XPlat Code Coverage" --results-directory $(Build.SourcesDirectory)/TestResults'
- task: DotNetCoreCLI@2
displayName: 'Generate coverage report'
inputs:
command: 'custom'
custom: 'reportgenerator'
arguments: '-reports:"$(Build.SourcesDirectory)/TestResults/**/coverage.opencover.xml" -targetdir:"$(Build.SourcesDirectory)/coverage" -reporttypes:"HtmlInline_AzurePipelines;Cobertura"'
- task: PublishCodeCoverageResults@2
displayName: 'Publish coverage'
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/coverage/Cobertura.xml'- task: DotNetCoreCLI@2
displayName: 'Run tests with coverage'
inputs:
command: 'test'
arguments: '--settings coverage.runsettings --collect:"XPlat Code Coverage" --results-directory $(Build.SourcesDirectory)/TestResults'
- task: DotNetCoreCLI@2
displayName: 'Generate coverage report'
inputs:
command: 'custom'
custom: 'reportgenerator'
arguments: '-reports:"$(Build.SourcesDirectory)/TestResults/**/coverage.opencover.xml" -targetdir:"$(Build.SourcesDirectory)/coverage" -reporttypes:"HtmlInline_AzurePipelines;Cobertura"'
- task: PublishCodeCoverageResults@2
displayName: 'Publish coverage'
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/coverage/Cobertura.xml'undefinedundefinedundefinedundefined| Metric | New Code | Legacy Code |
|---|---|---|
| Line Coverage | 80%+ | 60%+ (improve gradually) |
| Branch Coverage | 60%+ | 40%+ (improve gradually) |
| Maximum CRAP | 30 | Document exceptions |
| High-risk methods | Must have tests | Add tests before modifying |
| 指标 | 新代码 | 遗留代码 |
|---|---|---|
| 行覆盖率 | 80%+ | 60%+(逐步提升) |
| 分支覆盖率 | 60%+ | 40%+(逐步提升) |
| 最高CRAP分数 | 30 | 记录例外情况 |
| 高风险方法 | 必须有测试 | 修改前先补充测试 |
coverage.runsettings| Pattern | Reason |
|---|---|
| Test assemblies aren't production code |
| Benchmark projects |
| Database migrations (generated) |
| Source generators |
| Compiler-generated code |
| Explicit developer opt-out |
| Generated files |
| Blazor component generated code |
| Blazor CSS isolation generated code |
| EF Core migrations (auto-generated) |
| Auto-properties (trivial branches) |
coverage.runsettings| 模式 | 原因 |
|---|---|
| 测试程序集不属于生产代码 |
| 基准测试项目 |
| 数据库迁移文件(自动生成) |
| 源生成器生成的代码 |
| 编译器生成的代码 |
| 开发者显式标记排除的代码 |
| 自动生成的文件 |
| Blazor组件生成的代码 |
| Blazor CSS隔离生成的代码 |
| EF Core迁移文件(自动生成) |
| 自动属性(无意义的分支) |