日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[翻译]NUnit---Description and Exception Attributes(十一)

發布時間:2024/8/23 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [翻译]NUnit---Description and Exception Attributes(十一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Description (NUnit 2.4)

Description特性給Test, TestFixture or Assembly應用一個描述性文字。這些文字會顯示在輸出的XML文檔中,在Test Property對話框也會顯示。

Example:

[assembly: Description("Assembly description here")]namespace NUnit.Tests {using System;using NUnit.Framework;[TestFixture, Description("Fixture description here")]public class SomeTests{[Test, Description("Test description here")] public void OneTest(){ /* ... */ }} } View Code

Note:Test and TestFixture特性支持一個可選的Description屬性。Description特性應該使用到新的應用程序。如果同時使用,Description特性優先級高。

ExpectedExceptionAttribute (NUnit 2.0 plus Updates)

可以使用這種方式來指定一個測試用例拋出期望的異常。這個特性有一些列定位和命名參數,我們會根據它的目標來分開討論。

Specifying the Expected Exception Type

初始特性是在NUnit2.0中引入,使用一個參數給出期望的精確的異常類型。例如:

[ExpectedException( typeof( ArgumentException ) )] public void TestMethod() { ...

從NUnit2.2.4開始,可以使用字符串的異常類型,避免定義程序集的引用。

[ExpectedException( "System.ArgumentException" ) )] public void TestMethod() { ...

以上兩種例子實現相同的功能,只有拋出System.Argument異常的測試用例執行成功。

Specifying the Expected Message

NUnit2.1引入了一個包含兩個參數的構造函數,指定異常的message屬性文本。NUnit2.2.4之后,相同的擴展使用一個字符串參數到構造函數。在NUnit2.4中,這些參數標記為已過時,并提供一個命名參數來替代。

// Obsolete form: [ExpectedException( typeof( ArgumentException ), "expected message" )] [ExpectedException( "System.ArgumentException", "expected message" )]// Prefered form: [ExpectedException( typeof( ArgumentException ), ExpectedMessage="expected message" )] [ExpectedException( "System.ArgumentException", ExpectedMessage="expected message" )]

在NUnit2.4,除了精確的匹配還可以指定異常信息附加額外的測試,通過使用枚舉類型的MatchType參數來實現,示例如下:

public enum MessageMatch {/// Expect an exact match Exact, /// Expect a message containing the parameter string Contains,/// Match the regular expression provided as a parameter Regex,/// Expect a message starting with the parameter string StartsWith }

如果沒有指定MatchType,會使用精確的匹配。

Specifying a Custom Error Message

在NUnit2.4,如果ExpectedException消息不滿足則可以指定一個自定義消息,使用UserMessage參數:

[ExpectedException( typeof( ArgumentException ), UserMessage="Custom message" )] public void TestMethod() { ...

Handling the Exception in Code
?

如果需要處理的異常太復雜,通常的做法是在測試代碼中使用try/catch快來處理。作為替代,NUnit2.4允許調用一個方法來處理異常。在需要用相同的方式處理多個異常時特別有效。

通常可以使用IExpectException接口來實現異常處理,示例如下:

public interface IExpectException {void HandleException( System.Exception ex ); }

只有通過標記為ExpectedException來調用異常處理程序。如果代碼中的異常類型等所有檢查都通過,特性可以不指定任何參數就指出期望的異常。

一個handler可以使用Handler參數指定給特定的方法

[ExpectedException( Handler="HandlerMethod" )] public void TestMethod() { ... }public void HandlerMethod( System.Exception ex ) { ... }

這個技巧可以不實現IExpectException或者混合使用就可以使用。在后面的例子中,指定處理程序可以應用到任何指定了的方法,而一般的異常處理程序適用于指定了ExpectedException的其他方法。

指定了之后,處理程序會堅持異常,并且斷言相關的屬性。在測試中的任何的失敗結果信息會與其他斷言保持一致的格式。

ExpectedException

namespace NUnit.Tests {using System;using NUnit.Framework;[TestFixture]public class SuccessTests{[Test][ExpectedException(typeof(InvalidOperationException))]public void ExpectAnExceptionByType(){ /* ... */ }[Test][ExpectedException("System.InvalidOperationException")]public void ExpectAnExceptionByName(){ /* ... */ }} }

?

?

轉載于:https://www.cnblogs.com/kim01/archive/2013/06/06/3122609.html

總結

以上是生活随笔為你收集整理的[翻译]NUnit---Description and Exception Attributes(十一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。