Translate

Friday 28 August 2009

Unit Testing .Net 4.0 with TDD.Net

Hey,

Even if NUnit in its current form does not support .Net 4.0, there is a workaround with Test Driven.Net. Download, install and VS 2010 acknowledges Test Driven.Net and you can run your unit tests right inside VS 2010. It is as cool as NUnit.

Here is the code to test the RandomGenerator class:

using System;
using NUnit.Framework;
using RandomCalculator;
namespace ClassLibrary1
{
[TestFixture]
public class Class1
{
[Datapoints]
public int[] parametersForRandomGeneration = RandomCalculatorClass.GenerateRandomNumbers();
[Theory]
public void RandomNumbers(int x, int y, int z)
{
Assume.That(z > 0 && z <4);
if (z == 1)
{
Assert.That(x+y, Is.EqualTo(RandomCalculatorClass.add(x,y)));
}
else if (z == 2)
{
Assert.That(x * y, Is.EqualTo(RandomCalculatorClass.multiply(x, y)));
}
else if (z == 3)
{
Assert.That(x / y, Is.EqualTo(RandomCalculatorClass.divide(x, y)));
}
prevNumber = z;
}
}
}

Happy Unit Testing !:)

No comments: