Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LearCore opened this issue Sep 22, 2023 · 8 comments
Closed

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

LearCore opened this issue Sep 22, 2023 · 8 comments
Labels
documentation duplicate This issue or pull request already exists enhancement General enhancement request

Comments

@LearCore
Copy link

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

@github-actions github-actions bot added the untriaged To be investigated label Sep 22, 2023
@horusceridian
Copy link

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

@Bertk
Copy link
Collaborator

Bertk commented Oct 19, 2023

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 Bertk added waiting for customer Waiting for customer action needs repro Needs repro to be investigated, cannot repro in local question This issue is a question and removed untriaged To be investigated needs repro Needs repro to be investigated, cannot repro in local labels Oct 26, 2023
@Bertk
Copy link
Collaborator

Bertk commented Oct 28, 2023

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"

@Bertk Bertk self-assigned this Oct 28, 2023
Copy link

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

@github-actions github-actions bot added the stale label Jan 28, 2024
@Bertk Bertk removed their assignment Mar 28, 2024
@github-actions github-actions bot removed the stale label Mar 31, 2024
@BodanGjozinski
Copy link

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:

  • Both of these achieve the same result in my project.
  • Which is to remove all views, migrations and the Infrastructure folder(its a duplicate in my case).
  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>
  • the ExcludeCodeCoverage.runsettings.xml file:
    `
<!-- Configuration for data collectors -->
<DataCollectionRunSettings>
	<DataCollectors>
		<DataCollector friendlyName="XPlat code coverage">
			<Configuration>

				<!-- This works -->
				<ExcludeByFile>**/*.cshtml,**/DAL/Migrations/*,**/MainApp.MVC/Infrastructure/**/*.cs</ExcludeByFile>
				
				<!-- Works now, for specific views folders -->
				<!--<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>-->
				
			</Configuration>
		</DataCollector>
	</DataCollectors>
</DataCollectionRunSettings>

`

  • the command i use:
    dotnet test --settings ExcludeCodeCoverage.runsettings.xml --collect:"XPlat Code Coverage"

Considerations for addition to documentation:

  • Add removal of views or all .cshtml files as an example.
  • Explicit mention of:
    • Only the first xml element <ExcludeByFile> is applied.
    • You can add more but they are ignored.
    • Instead, you need to concatenate different patterns with a ',' inside a single <ExcludeByFile>.

@Bertk Bertk added enhancement General enhancement request documentation and removed question This issue is a question waiting for customer Waiting for customer action labels Apr 7, 2024
@Bertk
Copy link
Collaborator

Bertk commented Apr 7, 2024

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

ExcludeByFile="$(ExcludeByFile)"

Coverlet does not support Microsoft coverage syntax Customize code coverage analysis

Copy link

github-actions bot commented Jul 7, 2024

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

@github-actions github-actions bot added the stale label Jul 7, 2024
@Bertk Bertk added duplicate This issue or pull request already exists and removed stale labels Jul 21, 2024
@Bertk
Copy link
Collaborator

Bertk commented Jul 21, 2024

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

@Bertk Bertk closed this as completed Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation duplicate This issue or pull request already exists enhancement General enhancement request
Projects
None yet
Development

No branches or pull requests

4 participants