The Wayback Machine - https://web.archive.org/web/20240607155055/https://github.com/microsoft/codecoverage/issues/12
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ExcludeFromCodeCoverageAttribute in a test framework scenario? #12

Closed
jzabroski opened this issue Mar 3, 2023 · 11 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@jzabroski
Copy link

Description

If I run dotnet test --collect="Code Coverage", then it counts xunit classes towards code coverage statistics.

Coverlet addresses this problem by providing an ExcludeByAttribute where the user can define arbitrary attributes to use for exclusion purposes.

Steps to reproduce

  1. Create a new .NET library project that has a class Foo that calculates A + B
  2. Create a new xunit test project that tests class Foo
  3. Run dotnet test --collect="Code Coverage"

Expected behavior

xunit assemblies not included in code coverage report.

Actual behavior

xunit assemblies included in code coverage report.

Diagnostic logs

n/a - please specify what you would want for this, and I can provide

Environment

Test SDK 17.5

@jzabroski
Copy link
Author

Note: Brad Wilson, xunit maintainer, said this is not a real concern for him, since xunit does not collect code coverage stats for the project today.

However, I am curious what the VSTest engineers have to say about this base case.

@MarcoRossignoli
Copy link
Contributor

cc: @jakubch1

@jakubch1 jakubch1 self-assigned this Mar 7, 2023
@jakubch1 jakubch1 transferred this issue from microsoft/vstest Mar 7, 2023
@jakubch1
Copy link
Member

jakubch1 commented Mar 7, 2023

Thanks for the report. We will work on excluding xunit dlls by default.

@jzabroski jzabroski reopened this Mar 7, 2023
@jzabroski
Copy link
Author

Oops. Hit close by accident while holding my newborn.

Just wanted to say that you should also think about Coverlet approach ExcludeByAttribute. For example, what about assertion libraries like Shoudly.

@jakubch1
Copy link
Member

jakubch1 commented Apr 4, 2023

In latest version https://dev.azure.com/dnceng/public/_artifacts/feed/test-tools/NuGet/Microsoft.CodeCoverage/overview/17.7.0-preview.23203.7 we added default exclusion of few libraries. We are working on some more generic approach like Coverlet has so not closing this.

@gioce90
Copy link

gioce90 commented Sep 28, 2023

We are working on some more generic approach like Coverlet has so not closing this.

Hi @jakubch1 , what does this approach consist of? Can you share some information about it? It would be nice to exclude from code coverage report all about third-party libraries (without using .runsettings files)

@jakubch1
Copy link
Member

We added some default exclusions for xunit and moq libraries. Generic solution is still not implemented.

@gioce90
Copy link

gioce90 commented Jan 19, 2024

I think this is related to #72

@jakubch1
Copy link
Member

In latest version https://dev.azure.com/dnceng/public/_artifacts/feed/test-tools/NuGet/Microsoft.CodeCoverage/overview/17.10.0-preview-24073-02 we ported from Coverlet feature to optionally exclude modules based on existence of sources.

To enable this feature in command line you can use:

dotnet test --collect "Code Coverage;ExcludeAssembliesWithoutSources=MissingAll"

If you use config/runsettings you can add:

<ExcludeAssembliesWithoutSources>MissingAll</ExcludeAssembliesWithoutSources>

under <Configuration> tag.

Remember that this is adding additional processing to check existence of sources. You will get always better performance by configuring correctly <ModulePaths> regexes.

@cremor
Copy link

cremor commented Jan 24, 2024

Shouldn't this be the default?
When would I ever care about coverage of an assembly for which I don't have any sources?

@jakubch1
Copy link
Member

I don't want to make it default because of performance. To check if sources exist you need to start with dll path => read dll metadata => read PDB data and get all sources => in case of deterministic build calculate original paths => check each file existence. This can take time for big solutions

For <ModulePaths> regexes you take dll path and check regex => no disk operations.

To calculate coverage using our tooling, sources are not needed so you can imagine scenarios that you build test projects on 1 machine then move binaries to several test nodes and execute them there. We are able to collect coverage in such scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants