coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.98k stars 386 forks source link

How to exclude .cshtml views from code coverage in coverlet #1537

Closed LearCore closed 2 months ago

LearCore commented 1 year ago

Hi I am having same issue i am not able to exclude Views- .cshtml from coverge My Folder structure in solution is .Web /Views- .cshtml files in the coverge result it is showing as AspNetCoreGeneraterDocumnet is 0 % . Please help which command should be used to exclude .cshtml pages.

I tried filter and exclude

horusceridian commented 1 year ago

facing the same issue with autogenerated code. tried to exclude them using runsettings file but no luck

Bertk commented 11 months ago

Hi, did you use one of this filters already?

I use the ExcludeByFile option in MSBuild targets file

dotnet vstest "$(_TestAssembly)" /collect:"XPlat Code Coverage" --ResultsDirectory:"$(_TestResultDirectory)" --logger:"trx;LogFileName=$(_TestResultTrxFileName)" --Diag:"%(TestToRun.ResultDiagLogfilePath);tracelevel=verbose" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile="**/*.Version.cs"

You find also a example for the class filter in coverlet build pipeline : Run tests with coverage

Bertk commented 11 months ago

The coberatura.xml file for a Razor Page project will have sections like:

        <class name="RazorPagesTestSample.Pages.ErrorModel" filename="Pages\Error.cshtml" line-rate="0" branch-rate="0" complexity="6">
          <methods>
...
            </method>
        </class>

This command will remove the *cshtml files from cobertura.xml report:

dotnet test src\RazorPages\RazorPagesTestSample.Tests\RazorPagesTestSample.Tests.csproj --collect:"XPlat Code Coverage" --results-directory="TestResults/" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile="**/*.cshtml"

github-actions[bot] commented 8 months ago

This issue is stale because it has been open for 3 months with no activity.

BodanGjozinski commented 6 months ago

Hey everyone,

Destaleling (should be a word) this one for clarity.
Sorry for the long post, I lost 2 days trying to figure this out 😄.

The filtering expression @Bertk provided works.
Thanks man 😄 .

I also had a problem when excluding the views as well as other folders.

I think there is no Issue with Coverlet here, but i might be wrong.
I am not exactly sure how the filtering expressions are supposed to work.

The situation arises when you try to exclude specific views using folder structure filtering syntax instead of all .cshtml files.

I think that the real issue is the folder structure of the Views folder, i.e. the filtering expressions people try out.

So if you try out his code:
<ExcludeByFile>**/Solution.Web/Views/*.cshtml</ExcludeByFile>
It works but it does not does not exclude all of the .cshtml files from the Views folder.

It excludes only the .cshtml files at the root level of the Views folder like _ViewStart.cshtml.
Which are almost always 2 files and they do not provide a noticeable change in coverage epecially when you think you are removing all .cshtml files.
It is easy to think that the whole exclusion does not work.
It does not exclude the files that he actually wants to exclude like Views/Users/AddOrUpdateUser.cshtml.

This is what works and what it does from what i have found:

Exclude all .cshtml files

<ExcludeByFile>**/*.cshtml</ExcludeByFile>

Exclude all .cshtml files from the MainApp.MVC/Views folder

<ExcludeByFile>**/MainApp.MVC/Views/*.cshtml,**/MainApp.MVC/Views/**/*.cshtml</ExcludeByFile>

Exclude all .cshtml fles from an Area Views folder

<ExcludeByFile>**/MainApp.MVC/Areas/Common/Views/*.cshtml,**/MainApp.MVC/Common/Views/**/*.cshtml</ExcludeByFile>

Tested Example:

  1. <ExcludeByFile>**/*.cshtml,**/DAL/Migrations/*,**/MainApp.MVC/Infrastructure/**/*.cs</ExcludeByFile>
  2. <ExcludeByFile>**/DAL/Migrations/*,**/MainApp.MVC/Infrastructure/**/*.cs,**/MainApp.MVC/Views/*.cshtml,**/MainApp.MVC/Views/**/*.cshtml,**/MainApp.MVC/Areas/Common/Views/*.cshtml,**/MainApp.MVC/Areas/Common/Views/**/*.cshtml,**/MainApp.MVC/Areas/IntranetPortal/Views/*.cshtml,**/MainApp.MVC/Areas/IntranetPortal/Views/**/*.cshtml,**/MainApp.MVC/Areas/PublicPortal/Views/*.cshtml,**/MainApp.MVC/Areas/PublicPortal/Views/**/*.cshtml</ExcludeByFile>

Considerations for addition to documentation:

Bertk commented 5 months ago

Coverlet supports only single XML element for filter parameter which could be a list of multiple values separated by comma.

https://github.com/coverlet-coverage/coverlet/blob/67e37bb28b5377f34d907fb13b6761818f365975/src/coverlet.msbuild.tasks/coverlet.msbuild.targets#L44

Coverlet does not support Microsoft coverage syntax Customize code coverage analysis

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 3 months with no activity.

Bertk commented 2 months ago

Solution available. See replies of this issue or similar closed issues with "cshtml" files e.g. #1375