HiveBrain v1.2.0
Get Started
← Back to all entries
debugcsharpCritical

How do I use Assert to verify that an exception has been thrown with MSTest?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
mstesthashowthrownwithverifyuseexceptionassertbeen

Problem

How do I use Assert (or other Test class) to verify that an exception has been thrown when using MSTest/Microsoft.VisualStudio.TestTools.UnitTesting?

Solution

For "Visual Studio Team Test" it appears you apply the ExpectedException attribute to the test's method.

Sample from the documentation here: A Unit Testing Walkthrough with Visual Studio Team Test

[TestMethod]
[ExpectedException(typeof(ArgumentException),
    "A userId of null was inappropriately allowed.")]
public void NullUserIdInConstructor()
{
   LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}

Code Snippets

[TestMethod]
[ExpectedException(typeof(ArgumentException),
    "A userId of null was inappropriately allowed.")]
public void NullUserIdInConstructor()
{
   LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}

Context

Stack Overflow Q#933613, score: 1115

Revisions (0)

No revisions yet.