ExpectedExceptionBaseAttribute クラス

単体テストで例外が発生することを想定するように指定する属性の基本クラスです。

継承階層

System.Object
  System.Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionBaseAttribute
      Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute

名前空間:  Microsoft.VisualStudio.TestTools.UnitTesting
アセンブリ:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 内)

構文

'宣言
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False, Inherited := True)> _
Public MustInherit Class ExpectedExceptionBaseAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public abstract class ExpectedExceptionBaseAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false, Inherited = true)]
public ref class ExpectedExceptionBaseAttribute abstract : public Attribute
[<AbstractClass>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)>]
type ExpectedExceptionBaseAttribute =  
    class
        inherit Attribute
    end
public abstract class ExpectedExceptionBaseAttribute extends Attribute

ExpectedExceptionBaseAttribute 型で公開されるメンバーは以下のとおりです。

コンストラクター

  名前 説明
プロテクト メソッド ExpectedExceptionBaseAttribute() ExpectedExceptionBaseAttribute クラスの新しいインスタンスを初期化します。
プロテクト メソッド ExpectedExceptionBaseAttribute(String) ExpectedExceptionBaseAttribute クラスの新しいインスタンスを初期化します。

このページのトップへ

プロパティ

  名前 説明
プロテクト プロパティ NoExceptionMessage インフラストラクチャ。
プロテクト プロパティ TestContext インフラストラクチャ。
パブリック プロパティ TypeId 派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 (Attribute から継承されます。)

このページのトップへ

メソッド

  名前 説明
パブリック メソッド Equals インフラストラクチャ。 このインスタンスが、指定したオブジェクトに等しいかどうかを示す値を返します。 (Attribute から継承されます。)
プロテクト メソッド Finalize オブジェクトがガベージ コレクションにより収集される前に、そのオブジェクトがリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。)
パブリック メソッド GetHashCode 対象のインスタンスのハッシュ コードを返します。 (Attribute から継承されます。)
パブリック メソッド GetType 現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。)
パブリック メソッド Match 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
プロテクト メソッド MemberwiseClone 現在の Object の簡易コピーを作成します。 (Object から継承されます。)
プロテクト メソッド RethrowIfAssertException AssertFailedException または AssertInconclusiveException の場合は、再び例外をスローします。
パブリック メソッド ToString 現在のオブジェクトを表す文字列を返します。 (Object から継承されます。)
プロテクト メソッド Verify インフラストラクチャ。

このページのトップへ

明示的インターフェイスの実装

  名前 説明
明示的なインターフェイス実装プライベート メソッド _Attribute.GetIDsOfNames 名前のセットを対応するディスパッチ識別子のセットにマッピングします。 (Attribute から継承されます。)
明示的なインターフェイス実装プライベート メソッド _Attribute.GetTypeInfo オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。 (Attribute から継承されます。)
明示的なインターフェイス実装プライベート メソッド _Attribute.GetTypeInfoCount オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 (Attribute から継承されます。)
明示的なインターフェイス実装プライベート メソッド _Attribute.Invoke オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。 (Attribute から継承されます。)

このページのトップへ

解説

独自の予期される例外の検証を実装することにより。 ExpectedExceptionAttribute クラスの組み込みメソッドでは処理できない次のような補足情報や要件を指定できます。

  • 例外の状態を確認しています。

  • 複数の種類の例外を予期します。

  • 間違った型の例外がスローされたときにカスタム メッセージを表示します。

  • 否定的テストの結果を制御します。

  • 属性の使用方法の詳細については、「属性を使用したメタデータの拡張」を参照してください。

次のクラスにはテスト対象のメソッドが含まれます。

using System;

namespace CSExample
{
    public class DivisionClass
    {
        private int fraction;

        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}

次のカスタム属性クラス ExpectedArithmeticException は、ExpectedExceptionBaseAttribute クラスから派生したクラスで、例外の種類とメッセージを検証します。 ExpectedArithmeticException 属性は、テスト メソッドが、System.ArithmeticException 型の例外または System.ArithmeticException から派生する型の例外をスローすることを確認します。 また、例外メッセージが指定した例外メッセージと一致することを確認します。 単体テストは、ArithmeticException から派生する DivideByZeroException をスローするため、成功します。また、指定した例外メッセージが、単体テストでスローされる例外のメッセージに一致します。

using CSExample;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
    public sealed class ExpectedArithmeticException : ExpectedExceptionBaseAttribute
    {
        private string exceptionMessage;
        private string wrongExceptionMessage;

        public string WrongExceptionMessage
        {
            get
            {
                return wrongExceptionMessage;
            }
            set
            {
                wrongExceptionMessage = value;
            }
        }

        public ExpectedArithmeticException(string expectedExceptionMessage) : this(expectedExceptionMessage, "No exception was thrown.")
        {
        }

        public ExpectedArithmeticException(string expectedExceptionMessage, string noExceptionMessage)
            : base(noExceptionMessage)
        {
            exceptionMessage = expectedExceptionMessage;
            WrongExceptionMessage = "The exception that was thrown does not derive from System.ArithmeticException.";
        }

        protected override void Verify(System.Exception exception)
        {
            Assert.IsNotNull(exception);

            // Handle assertion exceptions from assertion failures in the test method, since we are not interested in verifying those
            base.RethrowIfAssertException(exception);

            Assert.IsInstanceOfType(exception, typeof(System.ArithmeticException), wrongExceptionMessage);
            Assert.AreEqual(exceptionMessage, exception.Message, "Could not verify the exception message.");
        }
    }


    [TestClass()]
    public class DivisionClassTest
    {

        /* This test will pass because it thows a System.DivideByZeroException which derives from System.ArithmeticException. */
        [TestMethod()]
        [ExpectedArithmeticException("Attempted to divide by zero.", "An exception was expected, but no exception was thrown.", WrongExceptionMessage = "The wrong type of exception was thrown.")]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 5;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }

    }
}

スレッド セーフ

この型のすべてのパブリック static (Visual Basic では Shared) メンバーは、スレッド セーフです。 インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

参照

参照

Microsoft.VisualStudio.TestTools.UnitTesting 名前空間