FCI-Helwan blog

Just another FCI-H weblog

Unit Test

Testing, testing, and testing, the most important phase especially in case working with group

So, testing anything before delivering it, is equal to develop it itself. And to avoid what
happens in teams refer to TeamDevelopment to know.

In this article, I’ll demonstrate the basic test that applied on functions in particular public functions and the called Unit Test. (Private functions will be the next)

Walk with me to test these simple functions:

public static int Add(int op1, int op2)
{
return op1 + op2;

}
public static int Subtract(int op1, int op2)
{
return op1 – op2;

}
public static int Multiply(int op1, int op2)
{
return op1 * op2;
}public static int Divide(int op1, int op2)
{
return op1 / op2;

}
static void Main(string[] args)
{
Console.WriteLine(
“Add: “ + Add(5, 0));
Console.WriteLine(
“Subtract: “ + Subtract(5, 0));
Console.WriteLine(
“Multiply: “ + Multiply(5, 0));
Console.WriteLine(
“Divide: “ + Divide(5, 0));
}

Good, but something waiting you

So, I’ll try and catch the Divide function to not get this error.

But till now I give up fatal error, what about logic error, should I write a program to test my function to by passing different parameters each time, yes, I should, what Unit Test actually do is that plus some information.

So, in our Math library, right click on any public method then select Create Unit tests, a cute dialog appeared in tree mode (Project, namespace, Class, Members, Methods) we then select Divide method to apply unit test on it, NOO, do not press Ok, we almost done, below output project: select create a new visual C# test project, enter a name for this unit test project say, DivideMethodUnitTest.

Going to Solution Explorer, we notice a new project has been added, yes, it’s DivideMethodUnitTest test project. It contains a class called [YourClassNameTest]
open it

[TestMethod()]

public void DivideTest(){//some code}

It generates a method to test Divide method, and some code, we going to put our
edits

int op1 = 5; // TODO: Initialize to an appropriate value
int op2 = 0; // TODO: Initialize to an appropriate value

int expected = 0;
int actual;
actual = UnitTestSample.Math.Divide(op1, op2);

Right click on DivideMethodUnitTest test project and select Set as startUp project, Ctr + F5 Test result window opened to inform me that the test has been faild : double click on failed it opens new windows contains some information about what has happened

Error Message:
Test method DivideMethodUnitTest.MathTest.DivideTest threw exception: System. DivideByZeroException: Attempted to divide by zero..

So, what should I do now is to catch this error, after that I’ll go to retest Divide method, and Comment Assert.Inconclusive(“A method…..”);

int op1 = 5; // TODO: Initialize to an appropriate value
int op2 = 0; // TODO: Initialize to an appropriate value
int expected = 0;
int actual;
actual = UnitTestSample.Math.Divide(op1, op2);

Congrats, your test window tells you Passed, double click on passed, it tells you Standard Console Output: Attempted to divide by zero

By passing more parameters I can get the logic error and the unit test informs me about that
int op1 = 5; // TODO: Initialize to an appropriate value
int op2 = 1; // TODO: Initialize to an appropriate value
int expected = 2;
int actual;
actual = UnitTestSample.Math.Divide(op1, op2);

Again, the test has been failed double click on failed it opens new windows contains some
information about what has happened

Error Message:
Assert.AreEqual failed. Expected:<2>, Actual:<5>. UnitTestSample.Math.Divide did not return the expected value.

So, give the right op1, op2, and expected values, to check for logic error

Advertisements

February 10, 2007 - Posted by | C#

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: