NUnit预期异常 [英] NUnit expected exceptions
问题描述
我有一组测试用例,其中一些预期会抛出异常。因此,我已经设置这些测试的属性来预期如下所示的异常:
[ExpectedException(System.NullReferenceException )]
当我在本地运行测试时,但是,当我将测试移动到运行TeamCity的CI服务器时,所有我预期的异常的测试都会失败。这是一个已知的错误。
我知道还有 Assert.Throws<>
code> Assert.Throws NUnit提供的方法。
我的问题是如何使用这些而不是我目前使用的属性?
我已经看过了StackOverflow,并尝试了几件事情,没有一个似乎对我有用。
有一个简单的1行解决方案来使用这个方法吗?
我不知道你试过的是给你麻烦,但你可以简单地传递一个lambda作为Assert.Throws的第一个参数。这是我的一个测试通过的一个:
Assert.Throws< ArgumentException>(()=> pointStore.Store new [] {firstPoint}));
好的,这个例子可能有点冗长。假设我有一个测试
[Test]
[ExpectedException(System.NullReferenceException)]
public void TestFoo()
{
MyObject o = null;
o.Foo(); $ o.Foo()
$ b $ c>
然后你会删除 ExpectedException
将您的调用包含在 Assert.Throws
中的 o.Foo()
。
[测试]
public void TestFoo()
{
MyObject o = null;
Assert.Throws< NullReferenceException>(()=> o.Foo());
}
Assert.Throws
尝试调用代码片段,以便验证它抛出特定的异常。 ()=> DoSomething()
语法表示一个 lambda ,本质上是一个匿名方法。所以在这种情况下,我们告诉 Assert.Throws
来执行代码片段 o.Foo()
。
所以不,你不只是添加一行像你做一个属性;您需要在调用 Assert.Throws
中显式包装测试的将抛出异常的部分。您不必必须使用lambda,但这通常是最方便的。
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?
解决方案 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:
Assert.Throws<ArgumentException>(() => pointStore.Store(new[] { firstPoint }));
Okay, that example may have been a little verbose. Suppose I had a test
[Test]
[ExpectedException("System.NullReferenceException")]
public void TestFoo()
{
MyObject o = null;
o.Foo();
}
which would pass normally because o.Foo()
would raise a null reference exception.
You then would drop the ExpectedException
attribute and wrap your call to o.Foo()
in an Assert.Throws
.
[Test]
public void TestFoo()
{
MyObject o = null;
Assert.Throws<NullReferenceException>(() => o.Foo());
}
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 telling Assert.Throws
to execute the snippet o.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.
这篇关于NUnit预期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!