Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Ignore by attribute should also ignore inner / nested classes generated by lambda expressions #328

Closed
Sam13 opened this issue Jul 24, 2015 · 3 comments

Comments

@Sam13
Copy link
Contributor

Sam13 commented Jul 24, 2015

I have a class which I want to exclude from coverage with the System.Diagnostics.ExcludeFromCodeCoverage attribute. So I place that attribute over my class and add -excludebyattribute:System.Diagnostics.ExcludeFromCodeCoverageAttribute to the OpenCover command line.

If my class is generating inner classes e.g. caused due to lambda expression (e.g. Task or Parallel.For) these are getting covered. Since I cannot place an attribute to a lambda expression I have to rewrite all the code to avoid using lambda expressions. Which is quite annoying.

Even better would be that OpenCover would support: Ignoring inner classes when exclusion attribute is placed on class

Sample code:

[ExcludeFromCodeCoverage]
    public class Test
    {
        public void DoSomething()
        {
            InnerTest x = new InnerTest(10);
            int y = 11;
            int z = 12;
            var t = new Task(() =>
                             {
                                 // This block is still getting covered
                                 var temp = Calc1(x.x, y);
                                 Calc2(z, temp);
                             });
            t.Start();

            Parallel.For(1, 10, i =>
                                {
                                    // This block is still getting covered
                                    Calc2(z, Calc1(i, 12));
                                });
        }

        private int Calc1(int x, int y)

        {
            return x + y;
        }

        private int Calc2(int z, int y)
        {
            return z + y;
        }

        // This type is still getting covered
        private class InnerTest
        {
            public int x;

            public InnerTest(int x)
            {
                this.x = x;
            }
        }
    }
@Sam13 Sam13 changed the title Ignore by attribute should also ignore inner generated by lambda expressions Ignore by attribute should also ignore inner classes generated by lambda expressions Jul 24, 2015
@Sam13 Sam13 changed the title Ignore by attribute should also ignore inner classes generated by lambda expressions Ignore by attribute should also ignore inner / nested classes generated by lambda expressions Jul 24, 2015
@sawilde
Copy link
Member

sawilde commented Jul 24, 2015

Good idea - thanks

@Marat-Gumerov
Copy link

Marat-Gumerov commented Mar 4, 2021

I use OpenCover 4.7.922 and collect coverage for class library based on NETFramework 4.5. My tests use NETFramework4.8.
I use my own SomeNamespace.SomeExcludeAttribute and I added it to opencover as -excludebyattribute:*.SomeExclude*. I use this attribute on methods only. Looks like all code in method is excluded, but not inner lambda functions. Can you help me in understanding what is wrong in my case?

@sawilde
Copy link
Member

sawilde commented Mar 7, 2021

There's nothing wrong - it is probably the way that .net has generated the lambdas and they are not being recognised correctly.

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

No branches or pull requests

3 participants