如何将C#记录传递到NUnit测试中的TestCaseSource?

huangapple go评论68阅读模式
英文:

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

问题

在这个示例中,使用 nunit v3.13.3,我期望运行两个测试

[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);

在调试时,只有第一个案例传递给 MethodInTestProject。在测试资源管理器中,我得到以下输出

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

我有其他关于传递类、值类型等参数的示例,没有任何错误。是否有一种特定的方法来传递 C# 记录?

英文:

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?

答案1

得分: 0

你正在创建一个包含对象数组的源代码。尽管在技术上是允许的,但这给了 NUnit 几乎没有类型信息来解释源代码。使第二级数组可为空会进一步复杂化事情。该数组代表了方法的收集参数,不能为null。在你的情况下,你可以消除一级,因为该方法只接受一个参数。

然而,更好的方法是使用TestCaseData,这是 NUnit 类,专门用于保存关于测试用例的信息。类似于这样...

private static readonly TestCaseData[] _cases = new []
{
    new TestCaseData(null), // 假设你想将null传递给方法
    new TestCaseData(new RecordParameter(Guid.NewGuid()))
}
英文:

You are making a source which consists of an array of object arrays. Although
technically permissible, this gives NUnit almost no Type information to use when interpreting the source. Making the second level array nullable complicates things further. That array represents the collected arguments to a method and can't be null. In your case, you could eliminate one level since the method only takes a single argument.

However, a better approach would be to use TestCaseData, the NUnit class which is designed to hold the information about a test case. Something like this...

private static readonly TestCaseData[] _cases = new []
{
    new TestCaseData(null), // Assuming you want null passed to the method
    new TestCaseData(new RecordParameter(Guid.NewGuid()))
}

答案2

得分: 0

"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: https://stackoverflow.com/questions/45635267/c-sharp-nunit-testcasesource-cant-provide-unique-test-data-for-each-test-run"

英文:

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 https://stackoverflow.com/questions/45635267/c-sharp-nunit-testcasesource-cant-provide-unique-test-data-for-each-test-run

huangapple
  • 本文由 发表于 2023年4月19日 22:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定