Generic implementation of simple unit test in csharp

Unit-testing frameworks can help make helper methods more generic like this, so tests are written more easily.
Test implementation:

 
public class TestUtility
{
public static void ShowError(string test,string message )
{
string msg = string.Format(@"{0}-{1}", test, message);
Console.WriteLine(msg);
}
}
public static void TestReturnsZeroWhenEmptyString()
{
//use .NET's reflection API to get the current method's name
 
string testName = MethodBase.GetCurrentMethod().Name;
try
{
SimpleNumber p = new SimpleNumber(); //testing the simpleNumber method from previous example
int result = p.ParseAndSum(string.Empty);
if(result!=0)
{
//Calling the helper method
 
TestUtility.ShowError(testName,"Parse and sum should have returned 0 on an empty string");
}
}
catch (Exception e)
{
TestUtility.ShowError(testName, e.ToString());
}
}
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker