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

Code coverage should exclude auto generated entrypoint #813

Closed
codito opened this issue May 16, 2017 · 14 comments
Closed

Code coverage should exclude auto generated entrypoint #813

codito opened this issue May 16, 2017 · 14 comments

Comments

@codito
Copy link
Contributor

codito commented May 16, 2017

Description

Code coverage for .net core projects is including AutoGeneratedProgram in coverage metrics. It should be excluded.

Steps to reproduce

  1. Use these instructions to run code coverage for .net core in test explorer: https://github.com/Microsoft/vstest-docs/blob/master/docs/analyze.md#working-with-code-coverage

Expected behavior

Coverage includes user code only.

Actual behavior

image

Environment

VS 2017 15.3 Preview 1

@codito
Copy link
Contributor Author

codito commented May 16, 2017

Might get fixed with #792 which will remove auto generated code from test projects.

@codito codito added this to the 15.5 milestone Jul 7, 2017
@codito
Copy link
Contributor Author

codito commented Jul 7, 2017

Adding all coverage impacting issues for 15.5.

@samsmithnz
Copy link

I just came across this today. I believe it's related to #519.

Does marking it as an enhancement mean it's not going to be fixed (this is typically what enhancement means at my organization :P)

@mayankbansal018
Copy link
Contributor

mayankbansal018 commented Nov 20, 2017

CodeCoverage does not know that a certain code is autogenerated if it does not have "CompilerGenerated" Attribute on top of them.

This is by design

@peteri
Copy link

peteri commented Nov 20, 2017

The code is autogenerated by the msbuild step in vstest/src/package/nuspec/Microsoft.NET.Test.Sdk.targets installed by the nuspec package so an attribute should either be added as part of the autogenerated file or the autogeneration should be removed.

@mayankbansal018
Copy link
Contributor

@peteri reopening this issue. I'll take further look to understand why we need code, or how can we add the attribute

@peteri
Copy link

peteri commented Nov 20, 2017

This issue added the comments so style cop ignores it, I suspect that it probably just needs the attribute adding.

The other comments on the linked roslyn issue seem to suggest the autogenerated program.cs is still needed.

@samsmithnz
Copy link

It should have the GeneratedCodeAttribute attribute, not CompilerGeneratedAttribute though, right? Reference: https://blogs.msdn.microsoft.com/codeanalysis/2007/04/27/correct-usage-of-the-compilergeneratedattribute-and-the-generatedcodeattribute/

@mayankbansal018
Copy link
Contributor

@samsmithnz , thanks for bringing it up. I agree it should be GenerateCodeAttribute, we plan to resolve this in next release to Test Platform.,

@gingters
Copy link

@mayankbansal018 Any further infos on that?

@mayankbansal018
Copy link
Contributor

@gingters I've raised required PR's for this issue. We are hoping to resolve this in 15.6 RTM

@mayankbansal018
Copy link
Contributor

@gingters, For VS IDE we missed the deadline for 2017 update 6, this will get fixed in update 7
However, it is fixed for vstest xcopy nuget package that we ship

@mayankbansal018
Copy link
Contributor

fixed in latest Microsoft.Net.Test.SDK(15.8) VS 2017

@juniormayhe
Copy link

juniormayhe commented Nov 12, 2019

I think msbuild (or whatever visual studio 2019 uses) does not add ExcludeFromCodeCoverage tag in auto generated csharp files.

I was expecting to test project to build:

// <auto-generated> This file has been auto generated. </auto-generated>
using System;
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode]
class AutoGeneratedProgram {static void Main(string[] args){}}

After reading that lasted updates fix the problem, I changed

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />

to latest Test SDK:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />

and Tests.Program.cs is not being generated anymore at \obj\Debug\netcoreapp2.0 hence there is no code coverage metric being shown in Code Metrics Result.

So it seems to be possible to exclude from code coverage the autogenerated (Program) files of test project, by updating the Test SDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants