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

查看:31
本文介绍了使用 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天全站免登陆