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 预期异常
				
        
 
            
        基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
 - 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
 - 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
 - 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
 - JSON.NET 中基于属性的类型解析 2022-01-01
 - 如何动态获取文本框中datagridview列的总和 2022-01-01
 - 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
 - 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
 - 错误“此流不支持搜索操作"在 C# 中 2022-01-01
 - 全局 ASAX - 获取服务器名称 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				