Using the Testing Extensions

This project was created to add to the basic assertions that unit testing in Visual Studio (VS) provides. In VS, the way to do unit testing assertions is to write test code such as the following:

[TestMethod]
public void OldWay_Test()
{
   int number = 5;
   Assert.AreEqual(number, 5);
   Assert.IsTrue(number > 4);
   Assert.IsTrue(number > -1);
}

While this approach is sufficient for most cases, this project looks to provide a more readable way to write unit tests. This library contains a number or extension methods that allow you to perform assertions using the following syntax:

[TestMethod]
public void NewWay_Test()
{
   int number = 5;
   number.ShouldEqual(5);
   number.ShouldBeGreaterThan(4);
   number.ShouldBePositive();
}

Another feature of this library is the ability to chain assertions. Each assertion method returns the original source value, so the same test above can also be written the following way:

[TestMethod]
public void NewWay_Test()
{
   int number = 5;
   number.ShouldEqual(5) 
      .ShouldBeGreaterThan(4)
      .ShouldBePositive();
}

When an assertion fails, the failure notification is the same behavior that VS provides out of the box, displaying the assertion that caused the failure and the arguments associated with the assertion.

The other feature that this library provides is the ability to assert multiple expected exceptions in a single test method, rather than being limited to one expected exception like VS provides. You can provide the snippet to test for an exception via an Action() delegate. An example of this is shown below:

[TestMethod]
public void Testing_ExceptionsTest()
{
   int zero = 0;
   Testing.ShouldThrowException<DivideByZeroException>(() => { int y = 15 / zero; });
   Testing.ShouldThrowException<ArgumentNullException>(() => { throw new ArgumentNullException(); });
}

Last edited Feb 7, 2009 at 4:31 AM by jeremiahclark, version 1

Comments

No comments yet.