dotnet / dotnet/aspnetcore

Support exclusion of Razor Page code behind classes from code coverage

Open
#57,511 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I am trying to exclude the classes generated from the code behind of Razor pages from the code coverage report.

Generated classes use the `Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute` at assembly level, making it useless for "targeting" generated classes.

### Describe the solution you'd like

It would be helpful if generated classes could be decorated with a class-level attribute that can be targeted when excluding classes from code coverage.

The already existing `System.Runtime.CompilerServices.CompilerGeneratedAttribute` attribute would be enough.

### Additional context

```
[global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute("RouteTemplate", "/contacts/{contactId:guid}")]
[global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute("Identifier", "/Pages/Contacts/View.cshtml")]
[global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdateAttribute]
#nullable restore
internal sealed class Pages_Contacts_View : global::Microsoft.AspNetCore.Mvc.RazorPages.Page
```

The instances of `RazorCompiledItemMetadataAttribute` aren't useful as the same attribute is used with the `assembly` decorator at the beginning of the file.

```
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Sample.Pages.Contacts.Pages_Contacts_View), @"mvc.1.0.razor-page", @"/Pages/Contacts/View.cshtml")]
```

Contributor guide

Open the contributing guide

Research direction

Start with the generated Razor Page class and the assembly-level RazorCompiledItemAttribute shown in the issue, then trace the Razor code-generation entry point that produces them. The work is done when generated code-behind classes carry class-level CompilerGeneratedAttribute, allowing code coverage tools to exclude them without relying on the assembly-level attribute.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.