【问题标题】:how I attribute unit test with data driven and expected exception我如何将单元测试归因于数据驱动和预期的异常
【发布时间】:2013-07-23 11:25:56
【问题描述】:

我有一个单元测试功能,其属性为“数据驱动”。 它调用另一个预期会引发异常的函数。 如果我将第一个方法或两次都归因于“ExpectedException”,它会失败,并且不会继续数据源上的其他行。 我该怎么办?

【问题讨论】:

  • 只需在测试中添加一个catch和Assert.Fail?

标签: c# unit-testing attributes


【解决方案1】:

您可以使用 NUnit 中的 Assert.Throws() 方法来验证测试中的方法是否引发了异常。

Assert.Throws<FileNotFoundException>(() => ThisMethodThrowsFileNotFound());

ExpectedException 属性用于测试方法(具有Test 属性的方法)。用它装饰任何其他方法都没有效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多