Using the Assert Classes
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
Use the Assert classes of the UnitTestingFramework namespace to verify specific functionality. A unit test method exercises the code of a method in your development code, but it reports the correctness of the code's behavior only if you include Assert statements.
The Microsoft.VisualStudio.TestTools.UnitTesting namespace provides several kinds of Assert classes:
In your test method, you can call any number of methods of the Assert class, such as Assert.AreEqual(). The Assert class has many methods to choose from, and many of those methods have several overloads.
Use the CollectionAssert class to compare collections of objects, and to verify the state of one or more collections.
Use the StringAssert class to compare strings. This class contains a variety of useful methods such as StringAssert.Contains, StringAssert.Matches, and StringAssert.StartsWith.
The AssertFailedException exception is thrown whenever a test fails. A test fails if it times out, throws an unexpected exception, or contains an Assert statement that produces a Failed result.
The AssertInconclusiveException is thrown whenever a test produces a result of Inconclusive. Typically, you add an Assert.Inconclusive statement to a test that you are still working on to indicate it is not yet ready to be run.
An alternative strategy would be to mark a test that is not ready to run with the Ignore attribute. However, this has the disadvantage that you cannot easily generate a report on the number of tests you have left to implement.
If you write a new Assert exception class, having that class inherit from the base class UnitTestAssertException makes it easier to identify the exception as an assertion failure instead of an unexpected exception thrown from your test or production code.
Decorate a test method with the ExpectedExceptionAttribute attribute when you want the test method to verify that an exception you expect to be thrown by a method in your development code is indeed being thrown in that method.