Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Team Test API
ExpectedExceptionAttribute Class

Indicates that an exception is expected during test method execution. This class cannot be inherited.

Namespace: Microsoft.VisualStudio.TestTools.UnitTesting
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in microsoft.visualstudio.qualitytools.unittestframework.dll)

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple:=False)> _
Public NotInheritable Class ExpectedExceptionAttribute
	Inherits Attribute
Visual Basic (Usage)
Dim instance As ExpectedExceptionAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false)] 
public sealed class ExpectedExceptionAttribute : Attribute
C++
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple=false)] 
public ref class ExpectedExceptionAttribute sealed : public Attribute
J#
/** @attribute AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false) */ 
public final class ExpectedExceptionAttribute extends Attribute
JScript
AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false) 
public final class ExpectedExceptionAttribute extends Attribute

This attribute is used to test whether an expected exception is thrown. The test method will pass if the expected exception is thrown. The test will fail if the thrown exception inherits from the expected exception.

If a test has both an ExpectedException attribute and an Assert statement, either one can cause the test to fail. To determine whether the attribute or the statement caused the test to fail, double-click the test result to open the Results Details page for the test. For more information about test results, see Test Results Reported.

This attribute can be specified on a method. There can be only one instance of this attribute on a method.

For more information about how to use attributes, see Extending Metadata Using Attributes.

The following class contains the method to test:

C#
using System;

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

The following test method tests the Divide method of the DivisionClass object. It tests for the existence of a DivideByZeroException.

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

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

<TestClass()> _
Public Class DivisionClassTest
    <TestMethod()> _
    <<b>ExpectedException</b>(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
System.Object
   System.Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Distorts code coverage (VS2010/.NET4)      evoskuil   |   Edit   |   Show History
Causing an assertion to be propogated from the class under test, even when caught by this attribute, results in the subsequent lines of the test not being executed and therefore not being counted as executed. Even when the exception is the last line in the TestMethod, the closing bracket counts as an unexecuted line. Coverage on tests is useful for ensuring that tests aren't conditional and that everthing intended is executeed (so it matters). What would be better is an assertion helper to execute the method that is expected to raise the exception. This is the approach documented here:

http://blog.typemock.com/2009/03/how-to-check-exception-message-using-ms.html

Very elegant and simple solution, and shoud be implemented as such as Assert.Throws() in the framework.
Does not work in VS 2005 and VS 2008      Vaccanoll   |   Edit   |   Show History
This attribute does not work in 2005 or 2008. Not sure why, but it does not.

See this link for info about it (compared to NUnit)
http://mvwood.com/blog/8789/

See this link for a somewhat klugey work around:
http://blog.typemock.com/2009/03/how-to-check-exception-message-using-ms.html


This seems like it may work in VS 2010:
http://weblogs.asp.net/gunnarpeipman/archive/2009/07/25/expectedexception-attribute-in-visual-studio-2008-and-visual-studio- 2010.aspx
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker