gpt4 book ai didi

c# - 带有泛型的 NUnit 3

转载 作者:行者123 更新时间:2023-11-28 20:40:44 26 4
gpt4 key购买 nike

我在这里看到这篇文章 Parametric test with generic methods对于 NUnit 2.5:

[TestCase((int)5, "5")]
[TestCase((double)2.3, "2.3")]
public void TestRowTestGeneric<T>(T value, string msg)
{
Assert.AreEqual(value, ConvertStrToGenericParameter<T>(msg));
}

但看起来它不再适用于 NUnit 3.0 了?

对于这种情况,正确的方法是什么?

[Test]
[TestCase(0)]
[TestCase(FakeEnum.DefaultValue)]
public void should_expect_T_value<T>(T expectedValue)
{
var result = DoStuff<T>();
Assert.AreEqual(expectedValue, result);
}

谢谢

最佳答案

通用参数测试在 3.0 中仍然可以正常工作。以下测试通过:

[Test]
// These are all "zero" values.
[TestCase(0, true)]
[TestCase(TypeCode.Empty, true)]
[TestCase(StringComparison.CurrentCulture, true)]
// These are not
[TestCase(TypeCode.Byte, false)]
[TestCase(StringComparison.InvariantCulture, false)]
public void Value_IsEquivalentTo_Zero<T>(T value, bool expectedResult)
{
// Quick n dirty conversion of 0 to T
T zero = (T)(object)0;
Assert.AreEqual(expectedResult, value.Equals(zero));
}

关于c# - 带有泛型的 NUnit 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33997435/

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