ExpectedExceptionAttribute クラス

テスト メソッドの実行時に例外の発生が予測されることを示します。 このクラスは継承できません。

継承階層

Object
  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 NotInheritable Class ExpectedExceptionAttribute _
    Inherits ExpectedExceptionBaseAttribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ExpectedExceptionAttribute : ExpectedExceptionBaseAttribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false, Inherited = true)]
public ref class ExpectedExceptionAttribute sealed : public ExpectedExceptionBaseAttribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)>]
type ExpectedExceptionAttribute =  
    class 
        inherit ExpectedExceptionBaseAttribute 
    end
public final class ExpectedExceptionAttribute extends ExpectedExceptionBaseAttribute

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

コンストラクター

  名前 説明
パブリック メソッド ExpectedExceptionAttribute(Type) 予測される例外を使用して、ExpectedExceptionAttribute クラスの新しいインスタンスを初期化します。
パブリック メソッド ExpectedExceptionAttribute(Type, String) 予測される例外の種類と、その例外について説明するメッセージを使用して、ExpectedExceptionAttribute クラスの新しいインスタンスを初期化します。

このページのトップへ

プロパティ

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

このページのトップへ

メソッド

  名前 説明
パブリック メソッド Equals インフラストラクチャ。 このインスタンスが、指定したオブジェクトに等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode 対象のインスタンスのハッシュ コードを返します。 (Attribute から継承されます。)
パブリック メソッド GetType 現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。)
パブリック メソッド Match 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ToString 現在のオブジェクトを表す文字列を返します。 (Object から継承されます。)
プロテクト メソッド Verify インフラストラクチャ。 (ExpectedExceptionBaseAttribute から継承されます。)

このページのトップへ

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

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

このページのトップへ

解説

この属性を使用して、予測される例外がスローされるかどうかをテストします。 予測される例外がスローされた場合、テスト メソッドは合格になります。 スローされた例外が予測される例外から継承されている場合、テストは失敗します。

テストに ExpectedException 属性と Assert ステートメントの両方が含まれる場合、どちらかがテストの失敗の原因になっている可能性があります。 属性とステートメントのどちらがテストの失敗の原因となったかを判定するには、テストの結果をダブルクリックして [テスト結果の詳細] ページを開きます。 テスト結果の詳細については、「Test Results Reported」を参照してください。

この属性は、メソッドに対して指定できます。 この属性のインスタンスは、メソッドに対して 1 つだけ指定できます。

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

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

using System;

namespace MyCSNamespace
{
    public class DivisionClass
    {
        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}
Public Class DivisionClass
   Public Function Divide(ByVal numerator As Integer, ByVal denominator As Integer) As Integer
      Return numerator \ denominator
   End Function
End Class

次のテスト メソッドでは、DivisionClass オブジェクトの Divide メソッドがテストされます。 DivideByZeroException が存在するかどうかがテストされます。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyCSNamespace;

namespace MyCSTestProject
{
    [TestClass()]
    public class DivisionClassTest
    {
        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 4;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }
    }
}
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports MyVBProject

<TestClass()> _
Public Class DivisionClassTest
    <TestMethod()> _
    <ExpectedException(GetType(System.DivideByZeroException), "MyMessage")> _
    Public Sub DivideTest()
        Dim target As DivisionClass = New DivisionClass
        Dim numerator As Integer = 4
        Dim denominator As Integer = 0
        Dim actual As Integer
        actual = target.Divide(numerator, denominator)
    End Sub
End Class

スレッド セーフ

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

参照

関連項目

Microsoft.VisualStudio.TestTools.UnitTesting 名前空間