#750 – Use xUnit.net for Unit Testing

On Codeplex, you’ll find a project called xUnit.net–a unit testing framework that allows you to write and run unit tests against your code.

To use xUnit.net, you write test methods that invoke the methods that you want to test and then make various assertions about the expected results.

For example, assume that you have a Divide method:

        public static double Divide(double a, double b)

You can then use xUnit.net to write a test method that makes several assertions about what it expects the results of calling the Divide method to be.

    public class SomeTests
        public void DivideTest()
            Assert.Equal(3.0, MathClass.Divide(6.0, 2.0));
            Assert.InRange<double>(MathClass.Divide(1.0, 3.0), 0.3333, 0.3334);

            Assert.Equal(3.14, MathClass.Divide(Math.PI, 1.0));  // Fails!

After you build your project, you can use the xUnit test console to run your unit tests.  It will indicate how many tests passed or failed.