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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

Description (NUnit 2.4)

Description特性給Test, TestFixture or Assembly應(yīng)用一個(gè)描述性文字。這些文字會(huì)顯示在輸出的XML文檔中,在Test Property對(duì)話框也會(huì)顯示。

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特性支持一個(gè)可選的Description屬性。Description特性應(yīng)該使用到新的應(yīng)用程序。如果同時(shí)使用,Description特性優(yōu)先級(jí)高。

ExpectedExceptionAttribute (NUnit 2.0 plus Updates)

可以使用這種方式來指定一個(gè)測(cè)試用例拋出期望的異常。這個(gè)特性有一些列定位和命名參數(shù),我們會(huì)根據(jù)它的目標(biāo)來分開討論。

Specifying the Expected Exception Type

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

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

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

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

以上兩種例子實(shí)現(xiàn)相同的功能,只有拋出System.Argument異常的測(cè)試用例執(zhí)行成功。

Specifying the Expected Message

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

// 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,除了精確的匹配還可以指定異常信息附加額外的測(cè)試,通過使用枚舉類型的MatchType參數(shù)來實(shí)現(xiàn),示例如下:

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,會(huì)使用精確的匹配。

Specifying a Custom Error Message

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

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

Handling the Exception in Code
?

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

通常可以使用IExpectException接口來實(shí)現(xiàn)異常處理,示例如下:

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

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

一個(gè)handler可以使用Handler參數(shù)指定給特定的方法

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

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

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

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(){ /* ... */ }} }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/kim01/archive/2013/06/06/3122609.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。