NUnit.Adventures

Have been learning up NUnit nowadays and trying to use it for my Paladin project. It is a nice tool and I strongly encourage you to use it for your development projects. You may have to download it as a separate tool now but I heard it will be bundled with Visual Studio 2005 in future.

Using a tool like NUnit will speed up your testing activities (because it is automated) and it can provide you with the confidence for your work. Regression errors can also be picked-up using this tool when you introduce a change in your code that breaks existing code. Lastly, this tool is free so there is no excuse for your company to complain for not using it. :p

Now, lets look at a simple test case I built using the NUnit.Framework.


using System;
using Paladin.Core;
using Citadel.Components;
using NUnit.Framework;

namespace TestCases
{
[TestFixture]
public class TestEntity
{
decimal Id;
Category cat = null;
}
}




I'm using a library project for my test case.
You can use an EXE project if you want to.

Noticed the [TestFixture] attribute? That's to indicate to NUnit that the class is a test case. You can easily indicate what you want to NUnit with the sets of attributes available. Read the NUnit documentation for more information.

Next, I need some methods. I will add one to test the Insert capabilities of my Paladin baby.


[Test]
public void TestSingleAdd()
{
Console.WriteLine("\nRunning single insert ....");

cat = new Category();
cat.Sequence = 1;
cat.Name = "Test Insert";
cat.Description = "Test Description";
cat.CreatedDate = DateTime.Now;

Id = (decimal)cat.Update();
Assert.IsTrue(Id > 0,
"Failed to insert into database.");

Id = cat.CategoryID;
Assert.IsTrue(Id > 0,
"Failed to set id on CategoryID property");

Console.WriteLine(
"Auto generated primary key is " + Id);

}



And one more method to test the Update capabilities


[Test]
public void TestSingleEdit()
{
Console.WriteLine(
"\nRunning single update using MarkForUpdate() ....");

cat = new Category();
cat.CategoryID = Id;
cat.Name = "Updated using MarkForUpdate()";
cat.Description = "MarkForUpdate Description";
cat.MarkForUpdate();

object result = cat.Update();
Assert.IsNotNull(result, "Failed to update in database.");

Console.WriteLine(result.ToString() + " row affected.");

cat = null;
cat = new Category(Id);

Assert.AreEqual("Updated using MarkForUpdate()",
cat.Name, "Name not updated in database.");
Assert.AreEqual("MarkForUpdate Description",
cat.Description, "Description not updated in database.");

}



We are now ready to go!

But before we run the tests, let's look at the code. The [Test] attribute basically indicates that each method is to be runned by the nunit-gui tool. The Assert class used in this example is provided by NUnit and it contains many static methods that you can use. Here you can see that I used the IsTrue, IsNotNull and AreEqual methods. The rest of the code are just normal code that you will use to use a Paladin Entity object.

Now, compile the code.

To run the test, we can launch the nunit-gui tool and load in the assembly (DLL) that we just compiled. You can also get VS.NET to launch the tool for you but you need to configure some steps to enable that. You can read the docs for the steps but I will just keep it simple here.

After loading the assembly, click on the Run button to see the status of the tests. A Green bar indicates success and a red bar indicates otherwise. (Like my friend said, if you want to see some red, go sabo the code).

So thats how easy it is! Now, as a developer don't you prefer this than to run through stacks of papers, clicking here and there? ;)

No comments:

Post a Comment

Popular Post