The Importance Of Unit-Testing
Tags: testing, devops
Published: 2020-04-05 01:26:36

NOTE: For the purpose of getting my point across I will be demonstrating examples in Java, despite this the idea for unit testing remains constant across all languages.

#Before I discovered unit-testing
It wasn't till I tackled my first project that involved more than 3 different classes that normal debugging methods and just running and testing were just not doing it for me anymore. Each new class or module I wrote for my project added new layers of debugging atop of what I already had. This was a constant struggle for all the custom desktop applications I've worked on since then, at least until I discovered unit testing. 

#Unit-testing in a nutshell
Unit testing assures you that new code you develop functions as you want it to before integrating it within your main program as to avoid breaking something that already works.

#Making sure new code works before it breaks your old code
When developing large applications it's essential to test the separate pieces before you integrate all aspects of your project together, and when I say large I am referring to applications that require the usage of possibly 2 to 3 or more modules or classes.

Imagine you have a main program like the following:
```java
public class Main{
    public static void main(String[] args){
        String normal = "This is a normal string";
        String reverseString = reverse(normal);
        System.out.println(reverseString);
    }
    
    //Returns reversed string
    private static String reverse(String s){...}
}
```
Let's assume that this program functions perfectly in that it outputs a string in reverse order, however, you want to create your own custom type in order to reverse a string and store it along with other methods that might be related to reversing strings in an object called ReverseString.

You might have a temptation to integrate the new ReverseString object within the main program and tidy up some code but best practice is to assure yourself that your new code works as expected before integrating it. This would avoid the needless headache of having to trace bugs in your main program back to their origin, since if a bug is present, unit testing will allow you to see its origin faster than with tracing the program stack from the beginning.

Writing a unit test to test before you integrate is simple and can be achieved in these two steps:
1. Create a new module simply for the sake of testing
  * This module should stand alone and only rely on your new code
2. Write different methods within the module that execute different tests
   you wish to make on the new code you've written
  * Make sure that the methods also report successes and failures properly

Here is a unit test example for the main program above:
```java
import static org.junit.Assert.assertTrue;

public class ReverseStringTesting{

    @Test
    public void shouldReverse(){
        ReverseString reverse = new ReverseString("test string");
        assertTrue(reverse.equals("gnirts tset"));
    }    

}
```

Assuming all methods are properly defined within the ReverseString object, this test will run and the assertTrue would indicate whether it passes or fails. If it passes, we know that the string was in fact reversed. If it fails, then we know that there's a bug present within the ReverseString object because the output wasn't what we expected it to be.

#Wrap up
Unit testing may not be necessary for smaller projects but it can definitely save time and frustration when it comes to debugging larger projects if implemented correctly, as you'll have an idea of what's causing bugs and where. With that said, happy coding, debugging, and testing.