Writing simple test case

One of my friend asked me to write some example if we could say for test cases as he is going to interview for .NET Developer job and they propaly will ask him to do so which they did and a little snippet here let him pass the quistion so i decided to blog about it so may any one benfit

The thing is that for Developers job they usualy don't ask those big testing quistions or require you to write complix test cases but they certinly wanted to know that you have a bit background on the subject.

say you have a function that determine if the input is an even number

1
2
3
4
5
6
7
bool IsEven(int x)
{
if (x % 2 == 0)
return true;
else
return false;
}

In the line 3 i used % which will return the reminder of the division operation and checked if this match the 0 then the operation is for even number otherwise odd.

For testing such a function we will use the Assertion which provided through the namespace System.Diagnostics and the class Debug to write something like this.

1
2
3
4
Debug.Assert(IsEven(3) == true);
Debug.Assert(IsEven(0) == true);
Debug.Assert(IsEven(-5) == false);
Debug.Assert(IsEven(6) != IsEven(13));

If you intrested in the subject of Unit Testing i recommend you to take a read to Marc's Article . enjoy.

kick it on DotNetKicks.com

Published Mon, Dec 11 2006 1:21 PM by Adel Khalil
Filed under: , ,

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 5 and 3 and type the answer here: