NUnit expected exceptions(NUnit 预期异常)
问题描述
我有一组测试用例,其中一些预计会引发异常.因此,我已将这些测试的属性设置为预期会出现如下异常:
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")]
当我在本地运行测试时,一切都很好.但是,当我将测试转移到运行 TeamCity 的 CI 服务器时,所有预期异常的测试都会失败.这是一个已知的错误.
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.
我知道 NUnit 还提供了 Assert.Throws<>
和 Assert.Throws
方法.
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?
我查看了 StackOverflow 并尝试了一些似乎对我不起作用的方法.
I've had a look around StackOverflow and tried a few things none of which seem to work for me.
有没有简单的 1 行解决方案来使用它?
Is there a simple 1 line solution to using this?
推荐答案
我不确定你的尝试是否会给你带来麻烦,但你可以简单地将 lambda 作为第一个参数传递给 Assert.Throws.这是我通过的一项测试中的一项:
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();
}
这会正常通过,因为 o.Foo()
会引发空引用异常.
which would pass normally because o.Foo()
would raise a null reference exception.
然后您将删除 ExpectedException
属性并将您对 o.Foo()
的调用包装在 Assert.Throws
中.
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
尝试调用以委托表示的代码片段,以验证它是否引发了特定异常."() =>DoSomething()
语法表示一个 lambda,本质上是一个匿名方法.所以在这种情况下,我们告诉 Assert.Throws
执行代码片段 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()
.
所以不,你不能像添加属性那样只添加一行;您需要在对 Assert.Throws
的调用中显式包装将引发异常的测试部分.您不一定必须使用 lambda,但这通常是最方便的.
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 预期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NUnit 预期异常
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01