coverlet icon indicating copy to clipboard operation
coverlet copied to clipboard

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

Open J35P1N opened this issue 2 years ago • 5 comments

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.

J35P1N avatar May 09 '22 09:05 J35P1N

I think you must specify the runsettings file on the command line, there's no file that is picked up by default. Try adding --settings coverlet.runsettings, adapting the file name to your file and path if necessary. (from https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md#advanced-options-supported-via-runsettings)

petli avatar May 09 '22 09:05 petli

This isn't using the command line. It's an Azure Devops pipeline. I've already looked at a number of issues on here where people have had similar problems and they are advised to use the runsettings file and that it should be automatically picked up by the task but that is not the case. Furthermore, as it's running multiple test projects in one command, how does it identify where to retrieve the coverlet.runsettings file from? Would I need a runsettings file in every test solution under the command line option?

J35P1N avatar May 09 '22 09:05 J35P1N

I meant that you can add it to arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"' that gets built into the command line that is actually run.

I don't know if you can have different runsettings for different test projects, alas. If not, you can run each test project in a separate step, although that is of course a lot of extra setup.

petli avatar May 09 '22 12:05 petli

I tried that but upon build it is unable to find the runsettings file. I tried putting the file in the project root and even in each of the test projects but it doesn't find it. What syntax is needed to specify the exact path?

On Mon, 9 May 2022, 13:11 Peter Liljenberg, @.***> wrote:

I meant that you can add it to arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"' that gets built into the command line that is actually run.

I don't know if you can have different runsettings for different test projects, alas. If not, you can run each test project in a separate step, although that is of course a lot of extra setup.

— Reply to this email directly, view it on GitHub https://github.com/coverlet-coverage/coverlet/issues/1331#issuecomment-1121018205, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABD2437BLRPWGKBTS5X56ATVJD6GLANCNFSM5VNURU2Q . You are receiving this because you authored the thread.Message ID: @.***>

J35P1N avatar May 09 '22 12:05 J35P1N

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

github-actions[bot] avatar Sep 04 '23 00:09 github-actions[bot]