Export (0) Print
Expand All

Using the Assert Classes

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 on aspects of the code's behavior only if you include Assert statements.

NoteNote

With or without Assert statements, unit tests can generate code coverage data. For more information, see How to: Obtain Code Coverage Data.

Kinds of Asserts

The Microsoft.VisualStudio.TestTools.UnitTesting namespace provides several kinds of Assert classes:

Assert

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.

CollectionAssert

Use the CollectionAssert class to compare collections of objects, and to verify the state of one or more collections.

StringAssert

Use the StringAssert class to compare strings. This class contains a variety of useful methods such as StringAssert.Contains, StringAssert.Matches, and StringAssert.StartsWith.

AssertFailedException

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.

AssertInconclusiveException

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.

NoteNote

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.

UnitTestAssertException

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.

ExpectedExceptionAttribute

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.

Overloading Unsafe Types with Assert.AreEqual

The Assert.AreEqual method has many overloads that let you compare specific data types. However, the Assert.AreEqual method does not have an overload for unsafe types, such as the pointer data type.

To illustrate this, create a C# console application that contains the following class:

unsafe public class CUnsafeTest

{

private int* m_pX = null;

unsafe public void VoidPtr(void* voidPtr)

{

}

unsafe public int* ReturnPointer(float* fPtr, TestThis* pTestThis)

{

int x = 5;

return &x;

}

}

Now generate tests for the CunsafeTest class. For more information, see How to: Generate a Unit Test. You will see generated code similar to the following example:

[TestMethod()]

public void ReturnPointerTest()

{

unsafe

{

CodeGen.BVT.Unsafe.CUnsafeTest target;

target = new CodeGen.BVT.Unsafe.CUnsafeTest();

// TODO: Initialize to an appropriate value

System.Single* fPtr = null;

// TODO: Initialize to an appropriate value

TestThis* pTestThis = null;

// TODO: Initialize to an appropriate value

System.Int32* expected = null;

System.Int32* actual;

actual = target.ReturnPointer(fPtr, pTestThis);

Assert.AreEqual(actual, expected);

// On the preceding line, no overload is available.

}

}

In this case, you should replace the Assert.AreEqual statement with your own custom verification code.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft