gpt4 book ai didi

c# - 如何使用 NUnit (C#) 进行通用测试?

转载 作者:行者123 更新时间:2023-11-30 20:27:31 26 4
gpt4 key购买 nike

<分区>

我想用 NUnit 为两种类型编写一个通用测试作为测试用例。例如:

[TestCase(typeof(Class1))]
[TestCase(typeof(Class2))]

是否可以使用 Generics作为 TestCase 或类似的东西?我想避免重复的测试代码。如果我使用 Generics 那将是可能的分别为“设置”的类型。

感谢您的帮助。

[Test]
public void Test()
{
_repositoryMock.Setup(c => c.GetByPid<CloudMember>(localRoot.Pid)).Returns((CloudMember)null);

await _cut.Check();

Assert.IsFalse(localRoot.HasNeighbor);
}

我想为 c.GetByPid<CloudMember> 使用泛型.所以使用这样的测试用例c.GetByPid<TCase> .

代码可能如下所示:

    [TestCase(typeof(Class1))]
[TestCase(typeof(Class2))]
[Test]
public void Test()
{
_repositoryMock.Setup(c => c.GetByPid<T>(localRoot.Pid)).Returns((CloudMember)null);

await _cut.Check();

Assert.IsFalse(localRoot.HasNeighbor);
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com