Search code examples
c#parametersnunitrecord

How do you pass a c# record into an NUnit test with TestCaseSource?


In this sample using nunit v3.13.3 I expect two tests to be run

[TestCaseSource(nameof(_cases))]
public void MethodInTestProject(RecordParameter recordParameter)
{
    Assert.Pass();
}

private static readonly object[] _cases = new object[]
{
    new object?[]
    {
        null,
    },
    new object?[]
    {
        new RecordParameter(Guid.NewGuid()),
    },
};

public record RecordParameter(Guid StoreId);

When debugging only the first case is passed into MethodInTestProject. In test explorer I get the following output

 MethodInTestProject(RecordParameter { StoreId = 71cda514-3e76-4829-a244-1f39cd4a2532 })
   Source:MyTests.cs line 228

I have other examples of passing in classes, value types, etc in parameters without any errors. Is there a specific way to pass c# records?


Solution

  • After reviewing @Charlie 's answer I discovered the record parameter didn't have anything to do with the issue. The issue is related to the Guid.NewGuid() portion of the parameter. If that is changed to Guid.Parse("edce8430-e40e-440c-9b70-e5684eeac1e1") then the test runs fine.

    Details on why this works are available here c# nunit TestCaseSource can't provide unique test data for each test run