Freigeben über


Visual Studio Unit Testing Extensions v1.0.0.0

This is a personal hobby project that I have been working on recently.  The code has been released on CodePlex and can be found here.

The Visual Studio Testing Extensions is a library that exposes a set of extension methods to provide a way to write more readable unit tests in the Visual Studio Unit Testing environment. It is developed in C# using Visual Studio 2008.

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, I was looking 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();
 }

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(); });
 }

The source release for this project contains a complete suite of unit tests to validate the expected functionality that the library provides. In addition, the unit tests are a good example of the various extension methods that the library provides.

If you are interested in contributing to this project, please let me know.

Comments

  • Anonymous
    February 02, 2009
    PingBack from http://www.clickandsolve.com/?p=3089

  • Anonymous
    February 02, 2009
    Lovely idea Jeremiah and the code is well documented, well done.

  • Anonymous
    February 02, 2009
    Did you intend to use the "number" variable you created in OldWay_Test()??? i.e. Assert.AreEqual(number, 5); rather than Assert.AreEqual(5, 5);

  • Anonymous
    February 03, 2009
    danieldsmith: Thanks for the catch!

  • Anonymous
    February 03, 2009
    Last night I wrote a post about the Visual Studio Unit Testing Extensions that I put out on CodePlex

  • Anonymous
    February 10, 2009
    The comment has been removed

  • Anonymous
    February 12, 2009
    Sounds a lot like SpecUnit (written for nUnit, but just as usefull in MS Test): http://code.google.com/p/specunit-net/