Software development is not an easy task. There’s a lot of things to read, know and practice in order to be good. New techniques arise and older ones come back to life recently such as MVC. One of those new techniques out there is Unit Testing.
The case for Unit Testing is simple: to prove that your code actually behaves as it’s supposed to. But most of the time developers are reluctance to create unit testing for their applications. Here are some of the “reasons” I’ve heard of and what I think about them.
Unit Testing Is Complicated
Wrong. Most of the unit testing frameworks such as JUnit, PHPUnit or NUnit have similar functionality, methods and documentation. They differ mostly in installation and configuration. So if you know how to create a unit test suite with JUnit, you will pretty much know how to do the same with PHPUnit or NUnit.
Most of unit testing reduces to making assertions and knowing what to assert and where.
Unit Testing Takes Too Long
Wrong. This is the most common statement that I hear every time this topic comes up in a conversation. Most of the developers who have not done unit testing assume that they don’t have time for it because they’re already running late. But that’s a complete fallacy. They actually save time.
When a bug comes up in your code or something gets changed, without unit tests you cannot know easily figure out where the problem is and you’ll lose precious time making tracing and debugging chunks of code. With unit tests you are always sure that everything is working as it’s supposed to and if something stops working, you’ll find out quickly that there’s a problem.
As a last word you don’t need to run all the unit tests for your complete application. You can test certain parts of your code with the use of test suites and most of the command line tools allow you to filter the test you want to run.
I Don’t Need to Do Unit Testing, I Know Exactly What’s Happening in my Code
Wrong. There’s no way to prevent all possible failures such as null pointer exceptions, web services failures, language specific bugs, etc. With the tools that unit tests give you such as mock objects you can always know where the flow problem is and even bypass those errors if necessary. For example, if a web service fails, you can override your connection to point to your mock object and keep working until your service comes back, or simulate a test database, etc.
Unit Testing Is only for Big, Complicated, Bloated Software
Wrong. Even if you only display a single web page with some static content, you can find great use in unit tests. Some frameworks such as PHPUnit allow you to test for a specific status code, or if you dispatch a URL in the browser, or if the content you are rendering contains certain specific chunks of HTML syntax. So there’s no reason for you not to use unit testing even with simple software.
Unit Testing Is only for Testers
Very wrong. Quality Assurance engineers are by far the most underestimated people in software development. Their task is to assure there are no bugs, errors or unexpected behavior in the general application, not in each module that integrates the application. Each developer is responsible for making sure that his code works and behaves as required when it interacts directly with other modules. QA is responsible for integration testing, for making sure that the overall system behaves as required.
So if you help your QA crew by providing them a unit testing suite for your own code, they will love you, as you will be helping them understand your code and how it relates to other modules.
Feel free to experience the power of unit testing power yourself. There are alternative frameworks for each programming language and most of them are extremely easy to implement.
Also, read about techniques such as Test Driven Development, Continuous Integration, Extreme Programming (XP) and others. And follow the most important rule of all: keep learning. Read about and experiment with Unit Testing and then share your experiences with the community.
Please, let me know in the comments below if you would like more details about specific Unit Testing frameworks and I, or somebody else here, will write specific postings about them.