NUnit 预期异常
我有一组测试用例,其中一些预计会抛出异常。因此,我已经为这些测试设置了属性,以期望出现如下异常:
[ExpectedException("System.NullReferenceException")]
当我在本地运行测试时,一切都很好。但是,当我将测试转移到运行 TeamCity 的 CI 服务器时,所有预期异常的测试都会失败。这是一个已知的错误。
我知道 NUnit 还提供了 Assert.Throws
和 Assert.Throws
方法。
我的问题是如何使用这些而不是我当前使用的属性?
我浏览了 StackOverflow 并尝试了一些似乎对我不起作用的东西。
有没有一个简单的 1 行解决方案来使用它?
I've got a set of test cases, some of which are expected to throw exceptions. Because of this, I have have set the attributes for these tests to expect exceptions like so:
[ExpectedException("System.NullReferenceException")]
When I run my tests locally all is good. However when I move my tests over to the CI server running TeamCity, all my tests that have expected exceptions fail. This is a known bug.
I am aware that there is also the Assert.Throws<>
and Assert.Throws
methods that NUnit offers.
My question is how can I make use of these instead of the attribute I'm currently using?
I've had a look around StackOverflow and tried a few things none of which seem to work for me.
Is there a simple 1 line solution to using this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定您尝试过的操作会给您带来麻烦,但您可以简单地将 lambda 作为 Assert.Throws 的第一个参数传递。这是我通过的一项测试中的一个:
好吧,这个例子可能有点冗长。假设我有一个测试
可以正常通过,因为
o.Foo()
会引发空引用异常。然后,您可以删除
ExpectedException
属性,并将对o.Foo()
的调用包装在Assert.Throws
中。Assert.Throws
“尝试调用代表为委托的代码片段,以验证它是否引发特定异常。”() =>; DoSomething()
语法表示一个 lambda,本质上是一个匿名方法。因此,在本例中,我们告诉Assert.Throws
执行代码段o.Foo()
。所以不,您不只是像添加属性那样添加一行;而是添加一行。您需要在对
Assert.Throws
的调用中显式包装将引发异常的测试部分。您不一定必须使用 lambda,但这通常是最方便的。I'm not sure what you've tried that is giving you trouble, but you can simply pass in a lambda as the first argument to Assert.Throws. Here's one from one of my tests that passes:
Okay, that example may have been a little verbose. Suppose I had a test
which would pass normally because
o.Foo()
would raise a null reference exception.You then would drop the
ExpectedException
attribute and wrap your call too.Foo()
in anAssert.Throws
.Assert.Throws
"attempts to invoke a code snippet, represented as a delegate, in order to verify that it throws a particular exception." The() => DoSomething()
syntax represents a lambda, essentially an anonymous method. So in this case, we are tellingAssert.Throws
to execute the snippeto.Foo()
.So no, you don't just add a single line like you do an attribute; you need to explicitly wrap the section of your test that will throw the exception, in a call to
Assert.Throws
. You don't necessarily have to use a lambda, but that's often the most convenient.这是一个使用这两种方法的简单示例。
如果您不想使用 lambda。
Here's a simple example using both ways.
If you don't want to use lambdas.
默认情况下,TeamCity 使用 NUnit 2.2.10,它没有 ExpectedException。检查 TeamCity“NUnit for NAnt”文档,了解如何将其更改为更现代的内容,包括 TeamCity 提供的特定版本列表。
By default, TeamCity uses NUnit 2.2.10, which doesn't have ExpectedException. Check the TeamCity "NUnit for NAnt" docs to see how to change it to something more modern, including the specific list of releases TeamCity provides.
NUnit 添加了新的 Record.Exception 方法。
如果您喜欢将 Acts 和 Asserts 分开,那么
Acts:
Assert:
NUnit has added a new Record.Exception method.
If you prefer to separate Acts and Asserts then
Act:
Assert: