gpt4 book ai didi

unit-testing - 使用日期时间的 NUnit 参数化测试

转载 作者:行者123 更新时间:2023-12-03 05:46:50 25 4
gpt4 key购买 nike

NUnit 不可能执行以下操作吗?

[TestCase(new DateTime(2010,7,8), true)]
public void My Test(DateTime startdate, bool expectedResult)
{
...
}

我真的很想在其中放置一个日期时间,但它似乎不喜欢它。错误是:

An attribute argument must be a constant expression, typeof expressionor array creation expression of an attribute parameter type

我读过的一些文档似乎表明您应该能够做到这一点,但我找不到任何示例。

最佳答案

您可以在 TestCase 属性中将日期指定为常量字符串,然后在方法签名中将类型指定为 DateTime

NUnit 会自动对传入的字符串执行 DateTime.Parse() 操作。

示例:

[TestCase("01/20/2012")]
[TestCase("2012-1-20")] // Same case as above in ISO 8601 format
public void TestDate(DateTime dt)
{
Assert.That(dt, Is.EqualTo(new DateTime(2012, 01, 20)));
}

关于unit-testing - 使用日期时间的 NUnit 参数化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7114604/

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