Start using Unit Testing today

Published on March 26, 2012

- in Blog, Projects & Know-How

Unit testing is a very debatable subject. Is unit testing necessary ? Is the extra time needed to implement unit testing in a project really paying off in the end ? Well… let’s see; one of the developers in Qubiz will tell us his perspective on unit testing.

Unit testing is the way through which a developer checks if the logic of his code behaves as it is intended to. For each piece of code a developer wants to test, he will write a test method.

Why should unit testing be used?

Every project needs a documentation, so that the stakeholder for that project can learn how the project works, unit testing is the project’s documentation for developers. It also allows the developer to execute the code right away and see if the expected result is returned. If a project’s unit is re-written it is easy to find out if the new code still returns a correct result.

When a developer thinks about the result a unit test should return on the piece of code it was written for, he inevitably thinks about the exception flows as well. In the case of API’s, if you write the unit tests first, the API will work the way you want it, so unit testing also helps you to get a better design.

Basically the main reason for using unit testing is the fact that you can stay assured that the unit is working the way you expect it to do.

When should unit testing be used?

The quick answer to this question is always, you should always use unit testing in your projects. Unfortunately this is easier to be said than to be done, but on the other hand, realizing you need unit tests at a later stage in the project can make the implementation difficult, maybe next to impossible.

There are two basic things one should know about unit testing

Unit testing should be done only for the logical behaviour of the application

Although unit tests are easy to write and have high execution speeds, writing them makes the development phase longer

The second point above raises an interesting issue, most projects need to be finished as soon as possible, so some developers could think of jumping over the unit testing phase. This could be a wrong choice, because if you write unit testable code and the unit tests associated with it, you might even finish faster. How so ?

when you have an issue somewhere in your code, you can easily write a test to check the expected result

when you integrate a new unit with your code, it might be out of hand to test it through the system, but with a unit test you can have the result instantly.

What is your opinion on the learning curve of unit testing?

There is no secret to writing unit tests, there are only some basic principles that one should follow so that it takes the full advantages of the unit tests instead of the getting stuck with them. The hard part is to write unit testable code, so that no dependency between the unit and the outside environment is unbreakable.