使用NUnit Assert.Throws方法还是ExpectedException属性? [英] Use NUnit Assert.Throws method or ExpectedException attribute?

查看:97
本文介绍了使用NUnit Assert.Throws方法还是ExpectedException属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这些似乎是测试异常的两种主要方法:

I have discovered that these seem to be the two main ways of testing for exceptions:

Assert.Throws<Exception>(()=>MethodThatThrows());

[ExpectedException(typeof(Exception))]

其中哪些会最好?一个提供其他优点吗?还是仅仅是个人喜好问题?

Which of these would be best? Does one offer advantages over the other? Or is it simply a matter of personal preference?

推荐答案

第一个允许您通过多次调用测试多个异常:

The first allows you to test for more than one exception, with multiple calls:

Assert.Throws(()=>MethodThatThrows());
Assert.Throws(()=>Method2ThatThrows());

第二个只允许您为每个测试功能测试一个异常。

The second only allows you to test for one exception per test function.

这篇关于使用NUnit Assert.Throws方法还是ExpectedException属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆