Search code examples
c#unit-testingcode-coveragexunitcoverlet

Exclude AspNetCoreGeneratedDocument Class from coverage report in coverlet


I have this class AspNetCoreGeneratedDocument.Views_Users__AddOrUpdateUser appearring in the code coverage result for each view in the Views folder,

Exemple: AspNetCoreGeneratedDocument.Views_Users__AddOrUpdateUser AspNetCoreGeneratedDocument.Views_Users__GetUser etc...

Assembly: Solution.Web Class: AspNetCoreGeneratedDocument

I want to exclude this folder using the .runsettings file:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <!-- Globbing filter -->
                    <ExcludeByFile>**/Solution.Web/Views/*.cshtml</ExcludeByFile>
                    <IncludeTestAssembly>false</IncludeTestAssembly>
                    <DeterministicReport>false</DeterministicReport>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

this is the result i want: this image is from dotCover it allows creating a filter easily enter image description here


Solution

  • A year an a half later and the correct answer is ...

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

    So @KA-Yasso code partially works.
    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.
    It does not exclude the files that he actually wants to exclude like Views/Users/AddOrUpdateUser.cshtml.

    The answer by @red is not meant for coverlet, it won't work, the .runsettings.xml file structure is different.

    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>

    Documentation:
    Considerations:
    • 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>.