Pex で使われている属性を調べてみた - PexAllowedExceptionFromTypeUnderTestAttribute

Pex で使われている属性を調べてみた - PexClassAttribute - お だ のスペース の続きです。
今回は、PexAllowedExceptionFromTypeUnderTestAttribute について調べてみました。

PexAllowedExceptionFromTypeUnderTestAttribute

Allowing Exceptions
Namespace は Microsoft.Pex.Framework.Validation です。
PexAllowedExceptionFromTypeUnderTestAttribute 自体のページは、URL が変わったのか、未だ作成されていないのか、存在しません。
この属性は、Pex 実行時に予想されている(発生しても良い)例外を指定する属性の様です。
生成されたテストには「ExpectedException」が付くようです。


PexAllowedExceptionFromTypeUnderTestAttribute だと、PexClassAttribute で Type Under Test に指定した型から、例外が発生する事が予想されていることを示す属性の様です。
コンストラクタでは、allowedExceptionType と acceptExceptionSubtypes を指定出来ます。
allowedExceptionType:必須です。名前の通り、許可する例外の型を指定します。Null を渡すと ArgumentNullException が発生します。
acceptExceptionSubtypes:任意です。これも名前から察しがつくように、allowedExceptionType で指定した例外の型のサブクラスも許可するかを指定します。これを指定する場合は、true を設定しろと書かれていた*1ので、デフォルトは false(サブクラスは許可しない) になっているようです。


この例外を指定する属性は、他にも適用対象を取れるようです。
PexAllowedExceptionAttribute:どんな場合でも例外を許可する
PexAllowedExceptionFromAssemblyAttribute:指定したアセンブリから発生した例外は許可する
PexAllowedExceptionFromTypeAttribute:指定した型から発生した例外は許可する
PexAllowedExceptionFromTypeUnderTestAttribute:今回のやつ。PexClassAttribute で指定した Type Under Test の型から発生した例外は許可する
PexAllowedContractRequiresFailureAtTypeUnderTestSurfaceAttribute:PexClassAttribute で指定した Type Under Test の型で、Contract で 設定した Required(事前条件)違反 を許可する?(原文:allows a precondition violations on calls at the surface of the type under test.))※訳は自信無いので原文載せてます

*1:Visual Studio から定義へ移動で表示されたソース内のコメント