What is Unit Testing? Why You Should Learn It?

Reza latifi
3 min readDec 26, 2019

--

You may be a student, self-teaching or have a job and need to learn the Unit test because your company is going to use that. I`ll give you the conceptual ideas about the Unit test to be able to at least explain what it is at a job interview or to a friend. I am going to only introduce the essential basics, fast and easy.

I`m a Back-end developer so I`m familiar with “Manual” testing which many developers use to do. Manual testing is the first thing that comes to your mind when you want to do a TEST. let’s imagine you want to add some new features to the project you are working at, you write the code, check if the code is working as expected, publish the new code. This way of testing has some limitations. Your project is getting bigger and more complex, thus is it getting harder and harder to test all possible scenarios as it is growing.

So, is it possible to test all possible scenarios? what can we do instead of Manual testing? The answer is Automatic testing. If you have tests written specifically for each independent part of your code which runs programmatically and automatic to test a wide range of things, you can easily test your whole project and get a trustworthy result. using Unit tests makes it easy to find bugs and conflicts before committing your code into the project.

In order to use Unit testing in your projects, you have to follow the principles of using small components that can be tested individually, just like pieces of a puzzle. To test a component, it should be tested independently, which means the component should have no connection to other components such as Database, file, or other parts of code. But wait, if all components have to be independent, how are they going to work together? Absolutely it is impossible to have isolated components working together, but at the test stage, you can simulate other components connected to the testing one to make sure they do not affect the one under test. Each of your components should only do ONE thing. If your component is going to fetch some data, process it and save it somewhere, it should be divided into smaller components that only do one job.

Now you know the basics, but how to write a Unit test for components? how to name the test functions? Basically, unit tests should test all aspects of a component and their name should describe what they do. let’s make a unit test on the following example:

class Student{
/* variables */
var $name;

/* functions */
function setName($name){
$this->name = $name;
}

function getName(){
return $this->name."<br/>";
}
}

Unit Tests

// this is just an example of a test class structure
final class StudentTest extends TestCase{
public function testNameCanBeSet(): void{
$student = new Student;
$student->setName('drinkingGuy');
$this->Assert->areEqual($student->name, 'drinkingGuy');
}
public function testNameCanBePrinted(): void{
$student = new Student;
$student->name = 'drinkingGuy';
$this->Assert->areEqual($student->getName(),'drinkingGuy<br/>');
}
}

The Student class has 2 functions, each of them should be tested individually and we need to make sure that other functions are not affecting the function under test. For example, in testNameCanBePrinted function, I manually set value of name and did not use setName function.

In the end, there are lots of Unit test frameworks around which are listed here:

https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks

--

--