NUnit ExceptionAsserts documentation列出了几种表格。除了超载,这些表格可用:
Assert.Throws(/* params here, incl. delegate w/ code */)
Assert.Throws<T>(/* params here, incl. delegate w/ code */)
Assert.DoesNotThrow(/* params here, incl. delegate w/ code */)
我在这里错过了一个,这也是我所期待的:
Assert.DoesNotThrow<T>(/* params here, incl. delegate w/ code */)
目前我只使用可用的DoesNotThrow
版本,并祈祷它永远不会隐藏 表示问题的类型的例外情况。这真的不太令人满意。
我真的想要在测试中相关的异常(例如Assert.DoesNotThrow<SqlException>
)和不是真的异常(例如NullReferenceException
)之间进行区分。这不是一件大事,因为非通用的DoesNotThrow断言不会影响,当测试将是红色/绿色时,但它会影响如何报告是红色的。
在我开始创建自己的Assert扩展程序来处理这个问题之前,我想问:我在这里遗漏了什么吗?我想要的表格是不是因为某种原因?是否通过其他NUnit位轻松实现了?
答案 0 :(得分:0)
评论和缺乏答案表明NUnit中只有没有这样的方法。
根据评论中的建议,您可以使用DoesNotThrow
非泛型方法,只需忽略其他类型的异常将显示与Assert失败相同类型的输出这一事实。
评论中的另一个建议是编写自己的帮助方法,如果有人关心这里有一个可能的版本:
public static void AssertDoesNotThrow<T>(NUnit.Framework.TestDelegate testDelegate) where T : Exception
{
try
{
testDelegate.Invoke();
}
catch (T exception)
{
Assert.Fail("Expected: not an <{0}> exception or derived type.\nBut was: <{1}>",
typeof (T).FullName,
exception.GetType().FullName);
}
}
对于冒犯断言的测试代表,你得到一个很好的“失败”和这种类型的输出:
Expected: not an <System.ArgumentException> exception or derived type.
But was: <System.ArgumentException>
对于意外错误,测试在异常本身上失败。
至于我自己,我想我会接受这样一个事实:这在vanilla NUnit中不存在,并继续使用非泛型版本。