如何使用 ExpectedException 属性强制执行异常消息
我认为这两个测试应该表现相同,事实上,我在我的项目中使用 MS Test 编写了测试,只是现在发现它并不像 NUnit 那样尊重预期的消息。
NUnit(失败):
[Test, ExpectedException(typeof(System.FormatException), ExpectedMessage = "blah")]
public void Validate()
{
int.Parse("dfd");
}
MS 测试(通过):
[TestMethod, ExpectedException(typeof(System.FormatException), "blah")]
public void Validate()
{
int.Parse("dfd");
}
无论我给 ms 测试发出什么消息,它都会通过。
如果消息不正确,有什么方法可以让 ms 测试失败吗?我什至可以创建自己的异常属性吗?我宁愿不必为发生这种情况的每个测试编写一个 try catch 块。
I thought these two tests should behave identically, in fact I have written the test in my project using MS Test only to find out now that it does not respect the expected message in the same way that NUnit does.
NUnit (fails):
[Test, ExpectedException(typeof(System.FormatException), ExpectedMessage = "blah")]
public void Validate()
{
int.Parse("dfd");
}
MS Test (passes):
[TestMethod, ExpectedException(typeof(System.FormatException), "blah")]
public void Validate()
{
int.Parse("dfd");
}
No matter what message I give the ms test, it will pass.
Is there any way to get the ms test to fail if the message is not right? Can I even create my own exception attribute? I would rather not have to write a try catch block for every test where this occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我们到处都使用这个属性,并且我们显然误解了第二个参数(我们感到羞耻)。
然而,我们肯定用它来检查异常消息。以下是我们使用此页面中的提示的内容。它不处理全球化或继承的异常类型,但它可以满足我们的需要。同样,目标是简单地 RR 'ExpectedException' 并将其与此类交换。 (糟糕的 ExpectedException 已被密封。)
We use this attribute all over the place and we clearly misunderstood the second parameter (shame on us).
However, we definitely have used it to check the exception message. The following was what we used with hints from this page. It doesn't handle globalization, or inherited exception types, but it does what we need. Again, the goal was to simply RR 'ExpectedException' and swap it out with this class. (Bummer ExpectedException is sealed.)
mstest 第二个参数是测试失败时打印出来的消息。如果抛出 formatException,mstest 将成功。我发现这篇文章可能有用
http://blogs.msdn.com/b/csell/archive/2006/01/13/expectedexception-might-not-be-what-you-ve-expected.aspx
That mstest second parameter is a message that is printed out when the test fails. The mstest will succeed if a formatexception is thrown. I found this post that may be useful
http://blogs.msdn.com/b/csell/archive/2006/01/13/expectedexception-might-not-be-what-you-ve-expected.aspx
@rcravens 是正确的 - 第二个参数是测试失败时打印的消息。为了解决这个问题,我所做的就是以稍微不同的方式设计我的测试。诚然,我不喜欢这种方法,但它确实有效。
@rcravens is correct - the second param is a message that is printed if the test fails. What I've done to work around this is craft my tests a little differently. Admittedly, I don't love this approach, but it works.
我通过添加对 contains、不区分大小写和 ResourcesType-ResourceName 组合的支持,扩展了 BlackjacketMack 对我们项目的回答。
用法示例:
这是更新后的类:
I extended answer from BlackjacketMack for our project by adding support for contains, case-insensitive and ResourcesType-ResourceName combinations.
Usage example:
Here is the updated class:
您可以使用 此项目 中的代码,如 本文 创建一个 ExpectedExceptionMessage 属性,该属性将与 MS Test 配合使用以获得所需的结果。
ms 测试(失败):
You can use code from this project explained in this article to create an ExpectedExceptionMessage attribute which will work with MS Test to get the desired result.
ms test (fails):
尝试这种现代方式:
Try this modern way:
我不久前写过这个,所以也许 VS2012 有更好的方法。
http://dripcode.blogspot.com.au/search?q=exception
I wrote this a while back, so maybe there is a better way in VS2012.
http://dripcode.blogspot.com.au/search?q=exception
试试这个。在此示例中,我有一条问候消息,它显示给定名称的问候消息。当 name 参数为空或 null 时,系统会抛出异常并显示消息。
ExceptionAssert.Throws
在 MsTest 中验证两者。Try This. In This example I have a greeting message that displays the greeting message for a given name. When the name parameter is empty or null system throws an exception with message. The
ExceptionAssert.Throws
verify both in MsTest.