在 MSTest 中,如何使用 [ExpectedException(typeof(ApplicationException))] 验证确切的错误消息
使用 MSTest 如何验证来自测试方法的确切错误消息?我知道 [ExpectedException(typeof(ApplicationException), error msg)]
不会比较来自我的测试方法的错误消息,尽管在其他单元测试框架中它正在这样做。
解决这个问题的一种方法是使用一些 try catch 块编写单元测试,但我再次需要多编写 4 行。
有没有最聪明的方法来检查错误消息。
干杯, 普里坦
Using MSTest how can I verify the exact error message coming from a test method? I know [ExpectedException(typeof(ApplicationException), error msg)]
doesn't compare the error message coming from my test method, though in other unit test framework it is doing.
One way to solve this problem is to write my unit test using some try catch block, but again I need to write 4 lines more.
Is there any smartest way to check the error message.
Cheers,
Pritam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以创建自己的 ExpectedException 属性您可以在其中
断言
抛出的异常
消息。代码
用法
You can create your own ExpectedException attribute where you can
Assert
the message of theException
that was thrown.Code
Usage
使用这个小帮助器类:
用法:
显式捕获异常的优点是,当另一个成员(例如在初始化期间)抛出异常时,测试不会成功。
Use this little helper class:
Usage:
The advantage of explicit catching exceptions is that teh test does not succeed when another member (e.g. during the initialization) throws the exception.
MSTest v2 支持 Assert.Throws 和 Assert.ThrowsAsync,它们返回捕获的异常。
以下是有关如何升级到 MSTest v2 的文章: https://blogs.msdn.microsoft.com/devops/2017/09/01/upgrade-to-mstest-v2/
以下是示例用法:
MSTest v2 supports Assert.Throws and Assert.ThrowsAsync which returns the captured exception.
Here is an article on how to upgrade to MSTest v2: https://blogs.msdn.microsoft.com/devops/2017/09/01/upgrade-to-mstest-v2/
Here is example usage:
流利断言 (NuGet)具有非常“语言自然”的语法,用于定义单元测试中的期望:
使用任何算法检查错误消息都有多种变体(
Where(e => ...
)以及检查内部异常及其消息。Fluent Assertions (NuGet) has a very "language natural" syntax for defining expectations in unit tests:
There are multiple variations for checking the error message with any algorithm (
Where(e => ...
) as well as checking into inner exceptions and their messages.在 MSTest 中没有内置的方法可以做到这一点。这大约是“优雅”的:
但是,您可以考虑将 xUnit.NET 的 Assert.Throws API 移植到自定义库 - 这就是我们所做的。
您还可以访问 Microsoft Connect 对此建议进行投票。
In MSTest there's no built-in way of doing it. This is about as 'elegant' as it gets:
However, you could consider porting xUnit.NET's Assert.Throws API to a custom library - that's what we did.
You could also go to Microsoft Connect an vote on this suggestion.
我正在寻找一种方法来使用 mstest 检查内部异常的存在和类型,我发现了这个问题。我知道这是一个 2 年前的话题,但由于我的解决方案不在这里,让我分享一下。
对我来说,解决问题的最优雅的方法是创建一个派生属性,这是我的(抱歉,注释和字符串是法语,这是我的自然语言,但它应该是显而易见的):
现在,使用带有命名参数的属性“预期异常”。使用我的属性,您可以检查是否存在内部异常、异常消息和内部异常、使用正则表达式来匹配消息等...
您可以根据需要进行调整。
I was looking for a way to check the presence and type of inner exception with mstest and I found this question. I known it a 2 years old topic but since my solution is not here, let me share it.
To me, the most elegant way to solve the problem is to create a derived attribute, here's mine (sorry but comments and strings are in french, my natural language, but it should be obvious) :
Now, use this attribute with named parameters instead of the "ExpectedException". With my attribute you can check if there is an inner exception, message of the exception and inner exception, use a regex to match messages, etc...
You can adapt as you want.
注释和 try/catch 块的烦恼在于,测试的 ACT 和 ASSERT 阶段之间没有明确的分离。一种更简单的方法是使用实用程序例程“捕获”异常作为 ACT 阶段的一部分,例如:
这允许您执行以下操作:
The annoyance with annotations and try/catch blocks is that you don't have a clean separation between the ACT and ASSERT phases of the test. A simpler appraoch is to "capture" the exception as part of the ACT phase using a utitlity routine such as:
This allows you to do:
使用 MSTest,您无法做到这一点。
您已经知道此问题的解决方案:在 catch 块中断言异常消息。
With MSTest, you can't do this.
You already know the solution to this problem: assert the message of an exception in a catch block.
更新:哎呀..看到你想要在 MSTest 中使用它。对不起。速读与被你的标题误导了。
尝试一下 Callum Hibbert 的这个扩展项目看看它是否有效。
旧回复:
您可以使用 NUnit 2.4 及更高版本来执行此操作。
请参阅此处的 ExpectedException 文档
MatchType 可以是 Exact(默认)、Contains 或 Regex.. 这几乎可以处理 80% 的用例。如果验证变得太复杂,还有一种高级异常处理程序方法。个人从未使用过它。还不需要它。
Update: Oops.. see that you want this in MSTest. Sorry. Speed read & Misled by your title.
Try this extension project from Callum Hibbert and see if it works.
Old response:
You can do this with NUnit 2.4 and above.
See documentation of ExpectedException here
MatchType can be Exact (default), Contains or Regex.. which pretty much handles the 80% use-case. There is also an advanced exception handler method approach if the verification gets too complex.. never used it personally.. didn't need it yet.
MbUnit 也可以这样做:
MbUnit can also do this:
此代码在异步/等待场景中执行此操作:
代码参考
用法示例:
代码参考
这是同步情况的等效
注:这断言异常继承自给定类型。如果您想检查特定类型,您应该检查类型相等性,而不是使用
is
运算符。This code does it in the async/await scenario:
Code Reference
Example Usage:
Code Reference
This is the equivalent for synchronous situations
Note: this asserts that the exception inherits from a given type. If you want to check for the specific type you should check for Type equality instead of using the
is
operator.