Cannot cover C# Records properly

The screenshot is showing the SonarQube Cloud report.

Due to this behavior of SonarQube Cloud, I cannot get 100% code coverage. Not usually a big deal, but depending on the size of the Pull Request it varies the precision of code coverage percentage can vary a bit. More importantly, I need to feel the joy of having the pipeline report 100% coverage :slight_smile:

// Application Code
public record RemoveSystemUserInput
{
    public required Guid UserId { get; init; }
}

public Task<bool> RemoveSystemUserAsync(
    RemoveSystemUserInput input,
    CancellationToken cancellationToken) 
{
    /* code here */
    return true;
}
// Test code

// Arrange
var systemUser = fixture.GetSystemUser();

// Act
await repository.RemoveSystemUserAsync(
    new RemoveSystemUserInput
    {
        UserId = systemUser.Id
    },
    cancellationToken);

// Assert
await verify(result);

So it’s interesting to see code coverage shows that the property inside the record is covered, but not the top level of the record? Is it because there’s no constructor? I’m still using the new operator. ¯\(ツ)

  • ALM used: GitHub
  • CI system used: GitHub/ GitHub Actions?
  • Languages of the repository: C#
  • Steps to reproduce:
    • Use a record class type in C# and utilize it in the test project
    • cover all properties of the record
    • The record reports as not covered on the top level.
  • Potential workaround: use a class instead of record? Use records with constructors?

Hey there.

SonarQube Cloud just reads in the coverage report. If your coverage report shows it is uncovered/partially covered, SonarQube Cloud will mark it as such. You should see if your coverage tool has been updated to handle these constructs, and raise an issue with them if not.

1 Like

Awesome, thanks Colin!

I didn’t realize this, so that means I can see if I can fix it locally or else report to the coverage tool we’re using. Got it, I appreciate the quick response :slight_smile:

I found the issue, putting a link here in case anyone arrives to this thread from google etc.
[BUG] Records show no coverage · Issue #1633 · coverlet-coverage/coverlet

Overall SonarQube Cloud has been great, and P.S. the VS Code Extension, SonarQube for IDE is handy!

Have a nice day,

Nathanial

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.