coverlet: [BUG] Records show no coverage

Describe the bug With .Net 8, records show no coverage Tested with the nightly build too I read #1561 #1607 #1576

To Reproduce

public sealed record FolderInfoDto
{
    public bool IsHidden { get; init; }
    public long Creation { get; init; }
}
return new FolderInfoDto
{
    Creation = f.Creation,
    IsHidden = !string.IsNullOrEmpty(f.Mode) && 'h' == f.Mode[3]
};
internal abstract record BasicFolderDto
{
    public string? DfsMappedPath { get; init; }
    public string Path { get; init; } = null!;
}

internal sealed record CreateFolderDto : BasicFolderDto;

CreateFolderDto dto = new()
{
    Path = args != null && args.TryGetValue("path", out var path) ? (string)path : @"F:\rep1\rep2",
};
internal sealed record DeleteFolderDto : BasicFolderDto
{
    public bool Recurse { get; init; }
    public bool Force { get; init; }
}

DeleteFolderDto dto = new()
{
    Path = @"L:\path\",
};

Expected behavior FolderInfoDto, CreateFolderDto, DeleteFolderDto, BasicFolderDto are marked as covered.

Actual behavior They are marked as not covered.

Configuration (please complete the following information): Please provide more information on your .NET configuration: * Which coverlet package and version was used? 6.0.1 and nightly * Which version of .NET is the code running on? 8.0.2 * What OS and version, and what distro if applicable? Windows 11 * What is the architecture (x64, x86, ARM, ARM64)? x64 * Do you know whether it is specific to that configuration? No

Additional context

  • Same result from Visual Studio and CLI
  • No difference with either <TargetFramework>net8.0</TargetFramework> or <TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
  • removing abstract or sealed makes no difference
  • SkipAutoProps makes no difference

test.runsettings:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
	<RunConfiguration>
		<ResultsDirectory>.\TestResults</ResultsDirectory>
		<TargetPlatform>x64</TargetPlatform>
		<TargetFramework>net8.0</TargetFramework>
	</RunConfiguration>

	<DataCollectionRunSettings>
		<DataCollectors>
			<DataCollector friendlyName="XPlat code coverage">
				<Configuration>
					<Format>cobertura</Format>
					<Exclude>[coverlet.*.tests?]*,[*]Coverlet.Core*,[*]xunit*</Exclude>
				</Configuration>
			</DataCollector>
		</DataCollectors>
	</DataCollectionRunSettings>
</RunSettings>

ā— Please also read Known Issues

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 3
  • Comments: 16

Most upvoted comments

A hopefully useful additional data point

  • Visual Studio 2022 17.10.0
  • FineCodeCoverage 1.1.215
  • NUnit 4.1.0
  • coverlet.collector 6.0.2
  • C# 12
  • .NET 8

I have an internal record:

internal record Foo
{
    public string Bar() => "baz";
}

And an associated test project with a test:

[TestFixture]
internal class FooTests
{
    [Test]
    public void FooBarShouldReturnBaz()
    {
        var foo = new Foo();
        Assert.That(foo.Bar(), Is.EqualTo("baz"));
    }
}

After running the test, the method call shows that it was covered, but the record itself does not:

image

If I modify the record declaration to include the redundant primary constructor parentheses:

internal record Foo()
{
    public string Bar() => "baz";
}

and repeat the test, this time I get green all around:

image

Seeing this as well, hope this can be fixed

Same issue here. It seems if you would create your record like this:

public sealed record TestRecord
{
    public required string Test { get; } => "Hello";
}


var test = new TestRecord() with {};

the record definition shows as covered

I’m facing the same problem, having a large record causes pipeline failure.

Using () has a quick fix šŸ˜•

@Steve-OH thanks for sharing your findings. I hope developers can fix it soon. This issue has been around here for one year. Meanwhile I will add the parenthesis for record ctor.

Thanks for reporting and providing a repro. I see if I can get the discussion in #1576 started. I think we then would have less issues with records.