Skip to content

Records not initialized properly when used as data for data-driven test #11456

Description

Describe the bug

I have a record which includes some explicit properties that override automatic properties.

public record Size(double Width, double Height)
{
    public double Width { get; } = FilterNegative(Width);
    public double Height { get; } = FilterNegative(Height);

    private static double FilterNegative(double value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
    {
        ArgumentOutOfRangeException.ThrowIfNegative(value, paramName);
        return value;
    }
}

I am using this type in two ways in my unit tests:

  • Explicit creation in code:
    [TestMethod]
    public void TestMethod()
    {
      var foo = new Size(1, 2);
    }
  • Directly as dynamic data:
    public static IEnumerable<object[]> TestData()
    {
        yield return new object[] { new Size(1, 2) };
    }

Version used

  • MSTest version 4.4.1
  • Microsoft.NET.Test.Sdk version 18.10.0
  • Visual Studio 2026 version 18.10.1

Steps To Reproduce

  • When run from the command line dotnet test ..., all tests pass. Explicit properties Width and Height are properly initialized.
  • When run from Visual Studio (Test Explorer window, Run All Tests):
    • Records created in the test method are properly initialized.
    • Records created in the dynamic data have their explicit fields at the default value, zero.

At some point in the past, this was not the case. Under MSTest version 3.10.0, .Net 9, and a corresponding version of Visual Studio, running the tests from Visual Studio properly initialized the explicit properties, all the time.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions