question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExcludeFromCodeCoverage skipped for some lines in excluded method

See original GitHub issue

Hello! I use coverlet NuGet version 3.0.2

I have method marked as [ExcludeFromCodeCoverage], It’s async method. Some lines inside method skipped from excluding and are marked as uncovered, It’s on PrintScreen from report below code

`` [ExcludeFromCodeCoverage] public async Task<List<OrganizationStructureModel>> GetOrganizationStructureByPartnerAsync(Guid partnerId) { var topPartner = await _partnerService.GetTopPartnerAsync(partnerId);

        var filials = await GetFilialsByBusinessUnits().Where(i=>i.NewDicFilialExtensionBase.NewPartnerId == topPartner.Id)
                                                       .ToListAsync();

        var filialIds = filials.Select(i => i.NewDicFilialId).ToList();

        var businessUnits = await _db.BusinessUnitBase.Include(i => i.BusinessUnitExtensionBase)
                                                      .In(filialIds, i=> i.BusinessUnitExtensionBase.NewFilialid.Value)
                                                      .ToListAsync();

        return filials.Select(i => i.ToViewModel(businessUnits)).ToList();
    }

``

And printscreen from coverage report

Снимок экрана 2021-01-27 в 13 35 38

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoRossignolicommented, Oct 7, 2021

Hi all here,

at the moment coverlet try to follow the chain of caller(parent classes) in case [ExludeFromCodeCoverage] and also in my local test I’m not able to repro, looks like compiler does something new(organize types in a different way) and we need a simple repro to be able to fix this issue. You can find sample here https://github.com/coverlet-coverage/coverlet/tree/master/test/coverlet.core.tests/Samples ExcludeFromCoverage.IssueXXX

0reactions
StingyJackcommented, May 6, 2022

@MarcoRossignoli - Does this work for an example?

In this code, the GetClaimTypeValue function has all statements being reported as uncovered. The GetClaimValue that is below it is properly excluded from the analysis.

       /// <summary>
        ///     Gets the value of given claim type available in the httpcontext
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        /// <param name="claimType"></param>
        /// <returns></returns>
        [ExcludeFromCodeCoverage] 
        public static string GetClaimTypeValue(this IHttpContextAccessor httpContextAccessor, string claimType)
        {
            if (httpContextAccessor == null)
            {
                throw new ArgumentNullException(nameof(httpContextAccessor));
            }

            var claims = httpContextAccessor.HttpContext?.User?.Claims?.ToList();
            return claims.GetClaimValue(claimType);
        }

        /// <summary>
        ///     To get the value of given type from the list of claims
        /// </summary>
        /// <param name="claims"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [ExcludeFromCodeCoverage] 
        public static string GetClaimValue(this List<Claim> claims, string type)
        {
            if (claims == null)
            {
                return null;
            }

            var claimTypeValue = claims.Where(claim => claim.Type.Equals(type, StringComparison.OrdinalIgnoreCase)).Select(c => c.Value).FirstOrDefault();

            return string.IsNullOrWhiteSpace(claimTypeValue) ? null : claimTypeValue;
        }

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Can I exclude part of a method from code coverage?
I know I can exclude a class or method from coverage analysis with the [ExcludeFromCodeCoverage] attribute, but is there a way to exclude...
Read more >
How to exclude code from code coverage
What to exclude from code coverage? I think almost all applications have some classes we don't want to test. Usual candidates are primitive ......
Read more >
ExcludeFromCodeCoverageAttri...
Specifies that the attributed code should be excluded from code coverage information. public ref class ExcludeFromCodeCoverageAttribute sealed : Attribute. C#
Read more >
Ignore C# Code marked as ExcludeFromCodeCoverage from ...
So C# has the attribute [ExcludeFromCodeCoverage] to exclude this code from Coverage Analyse. Sa… ... danielhwe (Daniel Wehrle) September 28, 2022, 5:55am 1 ......
Read more >
Customizing Code Coverage Analysis - Visual Studio
Learn how to use the ExcludeFromCodeCoverageAttribute attribute to exclude test code from coverage results. You can include assemblies ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found