Dealing effectively with the code inherited 4 – Unit tests

This is the second part of the series (dealing effectively with the code inherited) in which we have to summarize the Book of the same name. You can view the complete articles series here.

We talked in previous articles about the importance of tests tests in general, and today we take the first step in detail. When I got to this part in my reading of the book I asked myself how do you write programs without writing tests.


We also talked in the previous article, specifically about the difference between the regression tests and unit tests. As our article today will deal with the deeper into the Unit tests.

We agreed to the tests is a code that writes the check code and make sure it works as expected. It also works as a safety net to the contract between the programmer and the code that it will work the same way. In the case of a modified work program or fix a bug, this will be the tests serve as a safety device should be programmed in the case that the new changes to the code led to a change in the action Code of the old.

Unit tests

The idea behind unit tests it scans the code and not as a whole but as parts components specific isolated for some, any this the components in isolation isolation. By this we mean the components – from the point of view of the unit tests – it is a smaller part of the program or system.

We need to understand the behavior of the program and the way it works, evaluated on the basis of the behavior of the processes independent of each other and as small as possible so that we can write a unit test for each of them individually.

In the end, these components are a code. In the code for the purpose of expansion of Object oriented code these units is about classes. In the active Procedural code be a of functions.

The question here is: Can we actually test the function or class in isolation from the rest of the code?

In the procedural systems, the test function in isolation from the rest of the code is often difficult. Because the way the sequence of code be that the function calls the function again and so even the simplest function in the system.

As in the Object oriented systems the choice of the classes in isolation from the rest of the classes within the code will be somewhat easier, but the fact that the classes do not live in isolation from each other. How many times have i/j writing class does not depend on any class else?

The concept of the isolation and to have the code in these components is as small as possible are the basics in the concept of the Unit testing.

Yes, the tests that cover large parts of the code are important, but they also carry some drawbacks such as:

1 – Error localization:

It is difficult to determine the source of the error when it covers the test a large area of the code. You should examine the variables and processes that criminalize them along the implementation of the code and all the requirements until it is determined the source of the error. So, what was the code covered by the test, the more branched the needs and grown up. Yes, we’ll do the same process in the unit tests to determine the source of any error but the effort is inevitably less, because we agreed that the unit tests cover the components as small as possible.

2 – Execution Time

The tests large need for longer time so that the test process. Probably will avoid programmers conduct the tests that significant time-consuming after each small adjustment in the code to avoid wasting time. But in the case of the unit tests, the examination will be limited to the tests small that cover the component that you the modification you just.

The Unit test of good is quick in its implementation, which helps in identifying the source of the problem.

Properties must not face in the Unit tests

Clearly, the Unit test must respect the following:

1- You must be quick in its implementation, and only cannot be considered as unit tests.

2- don’t have to communicate or communicates with the database database.

3- don’t have to try any operations on any network network.

4- don’t have to experience any communication with the file system.

5. don’t be considered unit tests if you as a programmer to try specific changes to the system to make it work, such as modifying the configuration file.

The test of the violation of the rules of the former is not bad, of course, but cannot be considered unit tests if they do not help to achieve the desired objective of writing this kind of tests.

Conclusion:

Any test well is to test useful. But the unit tests specifically help us in the protection of small details in the program and make sure it works as expected.

Always remember: the unit test is good should be fast and helps us in identifying the source of the problem bug easily.

 

Source: dealing effectively with the code inherited 4 – Unit tests

Leave a Reply

Your email address will not be published. Required fields are marked *