Golang Tutorial: Weight Conversion Tool

What?

s tutorial will introduce some key concepts to start using unit testing. Testing is a crucial part of developing reliable software; often, when a new feature or an update is needed, the only way to ensure backward compatibility after the change is running unit testing.

As the name suggests, a unit test tests a unit of code, a small portion, usually a function. This concept seems to be tricky, but it’s explained easily using an example from Math:

Lets say you have been ask to find a function that returns 6 when x = 2 and 18 when x = 4.

X Y
2 6
4 18

So, you come up with the following equation: $$f(x) = x^2 + 2$$

Then you have to test it x = 2:

$$\begin{eqnarray} f(2) &= x^2 + 2 \\ f(2) &= 2^2 + 2 \\ f(2) &= 4 + 2 \\ f(2) &= 6 \end{eqnarray}$$

Finally with x = 4:

$$\begin{eqnarray} f(4) &= x^2 + 2 \\ f(4) &= 4^2 + 2 \\ f(4) &= 16 + 2 \\ f(4) &= 18 \end{eqnarray}$$

Similar process is required when working with computer programs. First you receive a requirements then you develop a solution and finally you test it. The difference is that the testing can be programmed and executed automatically even everytime a change is done.