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

标签 unit-testing nunit

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 expression or 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/

相关文章:

c# - 是否可以访问项目外部的 SetUpFixture?

c# - 如何模拟存储库/工作单元

visual-studio - 单元/Specflow 测试运行 3 次

unit-testing - 用单元测试覆盖主函数中的代码

c# - DonorManagementTests 没有默认构造函数 (nunit/moq)

c# - 在 .NET Core 中运行 NUnit 测试

java - JUnit5 : Trouble Importing Assertions

c# - 如何验证一个事件已经被模拟取消订阅

c# - 提取属性参数值

c# - 在单元测试中使用 swisstopo.dll