13-04-2001
This document is a brief introduction to CSunit, a Unit Testing framework for C# programs, written in C#.
CSUnit is part of the DotGNU Portable.NET system that can be used to perform Unit Testing of C# programs. It has been designed with a minimum dependency on our C# library · Pnetlib , so that this can be used to test our library functionality.
A Unit Test is a simple piece of test code that tests one teeny-weeny bit of your program. A Unit Test does not test application level functionality. A Unit Testing tool is a framework that makes it Easy to write, maintain, execute, , collect & present results from, a number of unit tests.
With CSUnit, we can do all of the above. CSUnit comes with a a command line interface as of now. We will be building support into a number of IDE's like SharpDevelop or the DotGNU Development Environment later.
Using the CSUnit testing framework will involve the following:
using CSUnit;
using System;
public class TestFoo : TestCase
{
public TestFoo(String name) : base(name)
{
}
protected override void Setup()
{
// do the initial setup -- most likely none :)
}
protected override void Cleanup()
{
// cleanup the mess this test makes
}
// we consider any function that starts with a Test or test as a
// CSUnit test -- thank God for Reflection
public void TestFeature()
{
Assert("I'm testing Foo.Feature()",Foo.Feature() == expected_value);
// AssertEquals is especially good for testing String/Object retvals
AssertEquals("Foo.Feature() should be \"1\" here","1",Foo.Feature());
// OR be a bit more specific
AssertEquals("Foo.Feature()==\"1\"","1",Foo.Feature());
try
{
Foo a=null;
Foo.Feature("foo");
Fail("NullReferenceException not caught !");
// it's obvious that I went a little overboard with the
// error messages -- be a little less alarming :)
}
catch(NullReferenceException err)
{
//woooohooo caught it !.
}
}
}
Each unit test you write will be a method of this class, and should start with "Test" or "test"
Assert() is a function that is available via the TestCase class. The assert succeeds if the condition passed to it is true, and it fails otherwise. The AssertEquals() is a function that avoids the use of the == operator overloading for comparing Objects. When an assert fails, the CSUnit makes a note that the test has failed, and if a test routine returns without any error/failure, CSUnit makes a note that the test has succeeded.
Add as many unit tests as you like . Unlike the more famous JUnit, CSUnit does not support a GUI as of now -- but the really picky guys may contact me for a kewl hack on this issue.
What we have seen is the simplest way to write CSUnit tests. It is more than likely that testing non-trivial code will need non-trivial testing. I'll move into more detail later in a full fledged documentation effort for CSUnit. As well as the methods to run CSUnit tests.
CSUnit is a very simple tool that can be used to organize and easily execute a number of test cases, which can be built incrementally.This document did not cover all features of CSUnit but is only meant to be a first step into Unit testing using CSUnit.
Orginally for JUnit by Sriram Karra . Hacked for CSUnit by Gopal.V
This Document is Licensed under the GNU FDL.
(C) Southern Storm Inc 2002