NUnit 预期异常

发布于 2024-09-13 01:40:32 字数 408 浏览 3 评论 0原文

我有一组测试用例,其中一些预计会抛出异常。因此,我已经为这些测试设置了属性,以期望出现如下异常:

[ExpectedException("System.NullReferenceException")]

当我在本地运行测试时,一切都很好。但是,当我将测试转移到运行 TeamCity 的 CI 服务器时,所有预期异常的测试都会失败。这是一个已知的错误。

我知道 NUnit 还提供了 Assert.ThrowsAssert.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为人所爱 2024-09-20 01:40:32

我不确定您尝试过的操作会给您带来麻烦,但您可以简单地将 lambda 作为 Assert.Throws 的第一个参数传递。这是我通过的一项测试中的一个:

Assert.Throws<ArgumentException>(() => pointStore.Store(new[] { firstPoint }));

好吧,这个例子可能有点冗长。假设我有一个测试

[Test]
[ExpectedException("System.NullReferenceException")]
public void TestFoo()
{
    MyObject o = null;
    o.Foo();
}

可以正常通过,因为 o.Foo() 会引发空引用异常。

然后,您可以删除 ExpectedException 属性,并将对 o.Foo() 的调用包装在 Assert.Throws 中。

[Test]
public void TestFoo()
{
    MyObject o = null;
    Assert.Throws<NullReferenceException>(() => o.Foo());
}

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:

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.

岁月蹉跎了容颜 2024-09-20 01:40:32

这是一个使用这两种方法的简单示例。

string test = null;
Assert.Throws( typeof( NullReferenceException ), () => test.Substring( 0, 4 ) );
Assert.Throws<NullReferenceException>( () => test.Substring( 0, 4 ) );

如果您不想使用 lambda。

[Test]
public void Test()
{
    Assert.Throws<NullReferenceException>( _TestBody );
}

private void _TestBody()
{
    string test = null;
    test.Substring( 0, 4 );
}

Here's a simple example using both ways.

string test = null;
Assert.Throws( typeof( NullReferenceException ), () => test.Substring( 0, 4 ) );
Assert.Throws<NullReferenceException>( () => test.Substring( 0, 4 ) );

If you don't want to use lambdas.

[Test]
public void Test()
{
    Assert.Throws<NullReferenceException>( _TestBody );
}

private void _TestBody()
{
    string test = null;
    test.Substring( 0, 4 );
}
时间你老了 2024-09-20 01:40:32

默认情况下,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.

春夜浅 2024-09-20 01:40:32

NUnit 添加了新的 Record.Exception 方法。

如果您喜欢将 Acts 和 Asserts 分开,那么

Acts:

ex = Record.Exception(()={throw new Exception()} 

Assert:

Assert.NotNull(ex);

NUnit has added a new Record.Exception method.

If you prefer to separate Acts and Asserts then

Act:

ex = Record.Exception(()={throw new Exception()} 

Assert:

Assert.NotNull(ex);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文