Introduction to Unit Testing PDF Print E-mail
User Rating: / 0
PoorBest 
Monday, 09 March 2009
What is Unit Testing?

When we write programs we can not make them perfect from the first time. When you compile your project you face the first test for your application. The compiler checks the syntax and stops if it contains errors. This is a nice assistance but it does not end here. The program may contain bugs, and it CERTANLY contains bugs. Then we have to hunt them down and correct the application so the bug count is low enough. We can not correct all the bugs but we can reduce them to a decent level.

So here come the Unit Tests. They help us hunt the bugs by writing tests. So the tests are programs, methods actually. They test every part of our program to see if it works right.

Sometimes you just won't feel like writing tests, especially at first. It looks like writing tests is redundant. 'Why do I write additional code to test this? It will work!' Well the problem is that it will not! When we write even a simple class it often has errors. Even if we think that there can not be ones. And writing tests seems like slowing us down.

It is true that in terms of writing code it is more time consuming to write code and tests than only code. But in the long run it pays back. Not writing test will be faster only if you do not make any mistakes and there are no bugs. Remember that bugs emerge at places you thought that they will never appear. If you happen to write a non-trivial program with no bugs call me. I will see what I can arrange for the Guinness book.

More specific

Let me explain you in more details what the tests actually are. For example you have a class called Divider. It has a method to divide two numbers. So your test suite will contain tests to determine if the method works correct and to test the behavior of the method in special cases, in this case when the division is by 0.

In the end of our development cycle we will have thousands of tests (if the project is large). With a single click (even if it is not a single click it will be easy) we will be able to run the tests. And they will be executed automatically and the result shown to us. The result of a test is always true or false i.e. the test passed or it didn't.

Write tests first

The real difference here is not only that the tests are automatic and not performed by beta testers. Beta testers are still needed but for more limited purpose. Well, we actually write the tests BEFORE the code which will be tested.

That may seem little awkward but it has many benefits. First of them is that you will actually write the tests. The second one is that you think more thoroughly before writing the code. You have to think about what input will your methods get, what output they will return, what exactly will be their functionality. about the bugs.

Use of Unit Tests

Unit Testing is very important in the enterprise applications where the result is often very complex. If you leave too many bugs is such project the development will soon slow down. This contrivance is used in almost all serious companies. Just to name one - Microsoft.

The first serious use of unit tests was aided by JUnit - a framework easing the creation of unit tests. As the name suggests it was for Java. Yeah, Java was and still is the most widely used language in the enterprise environment. As I told you, unit tests are very important in the enterprise (of course, not only there). There are PyUnit - unit testing on Python, DUnit - unit testing for Delphi, and of course NUnit - unit testing for .NET and in particular C#. In my next article I will examine NUnit in detail.

Conclusion

Now you know when to use Unit Testing. When not to use them? You do not have to test anything unless you want it to work. Remember that :) Unit Tests give you the confidence that your program still works. You can check that at any moment of your development. That's why you will be more inclined to add new features and to enhance the old ones - you know you did not break anything. And if you did you know exactly where it is broken and you can repair it.

I think I convinced you that Unit Tests are good thing to know and to use. In fact they are one of the crucial things that make the difference between the good programmer and the wannabe-good programmer.

In my next article I will tell you about NUnit so you can start using unit testing immediately.


 
< Prev   Next >
School Joomla Templates and Joomla Tutorials