Unit Testing

by Ladbon

So this time I’ll be blogging about what Unit testing is and how I applied it to both my projects(BST and L.List).

Generally unit testing follows your functions you’ve created and makes sure they return the correct values or just does what they are suppose to do.

The way you configure them is dependent on what your functions does.

I will state here what kind of testing my class does and how it does so.

As a whole this is my unit testing:

 

bool are_equal(T a, T b)
{
return a == b;
}

template <class T>
bool verify(T expected, T got, const std::string& message)
{
if (are_equal(expected, got))
{
std::cout << message << “: ” << “PASSED” << std::endl;
return true;
}
std::cout << message << “Failed! Expected: ” << expected << ” Got: ” << got <<
” – ” << std::endl;
return false;
}

 

I send a specific value i expect to be returned from the function I check and the returned value. I also send a small message like “TESTING DELETION(): “.

So in order to lets say deletion to be checked I write the following:

 

//ERASE A L_LIST NODE
message = “\n\n\nTESTING ERASING INDIVIDUAL NODE AND SEARCH: “;
list.Erase_Individual_Node(data);
unit.verify(0, list.Search(5), message);

Here I send out a message, delete the individual node by using the function then use the unit testing class.
I send a ‘0’ to be expected because my function return the value 0 when it cannot find the node->value and send in the request to find the value I just deleted.
If I had failed I would have returned the data 5 which does not equal 0.

 

Its seriously just this easy. The code for the unit testing class was not written specifically by me, I only altered a few variables.

For more reading on unit testing:

http://en.wikipedia.org/wiki/Unit_testing
http://code.tutsplus.com/articles/the-beginners-guide-to-unit-testing-what-is-unit-testing–wp-25728
http://artofunittesting.com/definition-of-a-unit-test/