Tag Archives: Theoretical computer science

Getting started with Unit Testing

In this post I’ll talk about the concepts and theory of Unit Testing, shortly after the way to implement them. If you were thinking that the “algebra and such as” isn’t part of the practical programming then you were wrong! In this field the set of keyword contains only 1 important element – assert – which either generates an exception or not. Or, more precisely: you have a Groupoid defined on YourClass with (YourClass, assert).
…search engines and tutorials are not the key elements of this game, you will rather use your natural logic. 😉

So, what is Unit Testing about? I could paste here some Wikipedia definition, and after reading it you could ask yourself: why to test some parts of my program once it’s debugged and works correctly.

Imagine the following situation: you have an open-source project – because you are cool -. It’s source code is quite big, consists from 100 unique classes and 1000 definitions and holds 10000 lines of code. As I said, its open source, what means today you’ll hack something on the getFeedProperties() item, tomorrow I’ll and after a week a third person.

After a month when you are starting to package your software you discover that something is wrong with the program. It’s working, but in a little different way. Actually it’s showing the Feed’s URL instead of their desired name (yeah, it’s probably a feed reader).

What will you do? Of course debug it! You assume that the bug appears in the nth class. After checking the formatting parameters, the string output, etc. you discover that everything is fine in this class. No problem, backtrack it, look at the n-1th class and increment you personal wastedTime variable with 1 :P. Unfortunately in the n-1th everything is okay, so wastedTime++, take a look to n-2th class. After k steps you will reach the last class in your debugging, and you will found out that the getFeedProperties() returns the desired object, but the object’s name field is set to the feed’s URL.
Conclusion: problem solved with time requirement of O(n-k), it’s linear, not bad!
Could be it better?

What happens if your project has built with Unit Testing? When a third person submits his own patch for something what breaks something else, your automated Unit Test will report if some method is working different then it’s expected. So, if I should define: Unit Testing is about defining the behavior of different parts in your program between the given circumstances.

Take a look to this pseudo code:

// from the feed reader messed up by a 3rd person
FeedProperty getFeedProperties() {
...
}
...
// from FeedReaderTest test class
...
Feed f = new Feed("http://planet.ubuntu.com/rss20.xml", ....);

void testGetFeedProperties() {
...
FeedProperty fp = f.getFeedProperties();
...
assert fp.getFeedName == "Ubuntu"
//an exception will be generated here, because the getFeedProperties placed wrong string into the "name" field of your "f" object. Your class is inconsistent. The bug is obviously here.
}

Time requirement: O(1), the bug were found by running the top of your Unit Testing class.

Any thoughts, comments, spams are welcome!

Advertisements