Simple unit test in csharp

Class to be tested:

public class SimpleNumber
{
public int ParseAndSum(string numbers)
{
if(numbers.Length==0)
{
return 0;
}
if(!numbers.Contains(","))
{
return int.Parse(numbers);
}
else
{
throw new InvalidOperationException(
"I can only handle 0 or 1 numbers for now!");
}
}
}

Unit test for class:

class SimpleNumberTests
{
public static void TestReturnsZeroWhenEmptyString()
{
try
{
SimpleNumber p = new SimpleNumber();
int result = p.ParseAndSum(string.Empty);
if(result!=0)
{
Console.WriteLine(
@"SimpleNumberTests.TestReturnsZeroWhenEmptyString:
Parse and sum should have returned 0 on an empty string");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}

Running coded tests via a simple console application:

public static void Main(string[] args)
{
try
{
SimpleNumberTests.TestReturnsZeroWhenEmptyString();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker