Search code examples
asp.net-corecode-coveragecobertura

Cobertura - Coverlet.runsettings.xml values ignored by dotnetcore CLI test command


I am trying to generate code coverage for one of my solutions in an Azure DevOps build pipeline. Whilst I have the results of my tests appearing upon build completion along with the Cobertura code coverage report, there are a lot of files and namespaces I would like to exclude from the code coverage report as they are not testable units of code (e.g. models or database migrations).

I understand that I can utilise the coverlet.runsettings.xml file to do namespace exclusions but this does not appear to be working.

My runsettings file is setup inside one of the test projects that is run and is setup as follows:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <Exclude>[MyDatabase.Migrations.*]*,[*]MyDatabase.Migrations*</Exclude>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

I'm trying to exclude all files that sit under the MyDatabase.Migrations namespace but the syntax I have used above doesn't seem to have any effect and I still see the files beneath this namespace shown in final code coverage report in Devops.

My pipeline is setup as follows:

steps:
- task: DotNetCoreCLI@2
    displayName: 'Build'
    inputs:
    command: 'build'
    projects: '$(solution)'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
    displayName: 'Tests'
    inputs:
    command: 'test'
    projects: |
        **\MyDatabase.Tests
        **\MySearch.Common.Tests
        **\MySearchService.Tests
        **\MyConfiguration.Tests
    arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
    publishTestResults: true

# ReportGenerator extension to combine code coverage outputs into one
- task: reportgenerator@5
    inputs:
    reports: '$(Agent.WorkFolder)/**/coverage.cobertura.xml'
    targetdir: '$(Build.SourcesDirectory)/CoverageResults'

- task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
    inputs:
    codeCoverageTool: 'Cobertura'
    summaryFileLocation: '$(Build.SourcesDirectory)/CoverageResults/Cobertura.xml'
    reportDirectory: '$(Build.SourcesDirectory)/CoverageResults'

I am aware I can use the [ExcludeFromCodeCoverage] tag but with things that are auto-generated like migrations this could get pretty messy having to go in and modify them every time a new one is generated.

I would like the migrations folder completely omitted from even appearing in the generated report.

Any help with this issue would be greatly appreciated.


Solution

  • The VSTestIntegration documentation indicates you will need to include the custom runsettings file as an agrument:

    This runsettings file can easily be provided using command line option as given : dotnet test --collect:"XPlat Code Coverage" --settings coverlet.runsettings

    Try the following:

    arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" --settings [path to tests project]\coverlet.runsettings.xml'