FortuneN / FineCodeCoverage

Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too)
https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage
Other
516 stars 39 forks source link

Lambdas not excluded when using the [ExcludeFromCodeCoverage] attribute #350

Closed PeteCozens closed 11 months ago

PeteCozens commented 11 months ago

Installed product versions

Description

When using the [ExcludeFromCodeCoverage] attribute to exclude a class or method, lambda functions within the affected code are not excluded from code coverage and are marked as not covered

Steps to recreate

Taking the following code as an example:

        [ExcludeFromCodeCoverage]
        public static void Foo()
        {
            var tdm = typeof(IDataModel);
            var types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(tdm.IsAssignableFrom);

            foreach (var type in types)
            {
                // TODO...
            }
        }

Current behavior

The line containing .SelectMany shows as Not Covered because the lambda function (s => s.GetTypes()) is not excluded, even though the parent method (Foo) is excluded by virtue of the [ExcludeFromCodeCoverage] attribute

Expected behavior

When a class or method is excluded from code coverage, and lambdas contained therein should also be excluded.

tonyhallett commented 11 months ago

If you use the System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute then for both Ms code coverage and old style coverage ( OpenCover or Coverlet ) you will get the behaviour you expect.

image

Using your own ExcludeFromCodeCoverageAttribute does work with old style coverage. To be consistent we could set up ms code coverage to work in the same manner but the docs do state

You can ignore a method or an entire class from code coverage by creating and applying the [ExcludeFromCodeCoverage] attribute present in the System.Diagnostics.CodeAnalysis namespace.

Perhaps this should change to re-emphasise System.Diagnostics.CodeAnalysis.

Ms code coverage does provide a default Configuration / CodeCoverage element if not provided. It will also add some default exclusions if not present or merge them in unless you add the attribute mergeDefaults='false'. For instance it Attributes exclude ExcludeFromCodeCoverageAttribute. If you are interested see ...\AppData\Local\FineCodeCoverage\msCodeCoverageversion\build\netstandard1.0\Microsoft.VisualStudio.TraceDataCollector.dll and the DynamicCoverageDataCollector.

image