Unit testing in Python: Part 1
Unit testing is one of the most important topics every developer should understand while writing their scripts.
It is a software testing method where parts of the main code are put under different tests and situations. If it is successful then the code is ready to launch.
Why Unit Testing?
- Unit test discloses the problem early while developing. It prevents failures at the last moment.
- It allows the user to refactor the source code during the testing stage and later on and still get the expected output which makes sure it works
- It simplifies integration whereby testing separate components of the program and then tests it together, which makes integration easier.
- It also helps simplify the debugging process. If a test fails then only one part of the code that to the replica of it is affected.
This Unit Test framework is called unittest
Now let's try to solve an example before using the unit test functionality. We will write a code for finding the area of a square where we just take the length from the user as input.
First off we start by writing a function for the calculation:
def square_area(l):return (l*l)
Next, we make a list of inputs the system can receive:
length = [2j,1.1,-7,"three"]
Now the system will run the value in the list so here is the syntax for that:
for i in range(len(length)): print ("The area of a square:",square_area(length[i]))
Output:
The area of a square: (-4+0j)
The area of a square: 1.2100000000000002
The area of a square: 49
Traceback (most recent call last):
File "c:\Users\Harsh\Desktop\PYTHON\blog.py", line 5, in <module>
print ("The area of a square:",square_area(length[i]))
File "c:\Users\Harsh\Desktop\PYTHON\blog.py", line 2, in square_area
return (l*l)
TypeError: can't multiply sequence by non-int of type 'str'
PS C:\Users\Harsh\Desktop\PYTHON>
The output might give you the result but it will also warn the user that there might be some errors. This is where unit test plays an important role. The mistakes here are:
- One of the inputs was a complex number (2j).
- One of the inputs was a negative number (-7).
- The output shows
Type Error
where the system cannot multiply a string (three).
The third error was not successful but the other two still showed a result. But we know that they are incorrect inputs as a square cannot have a side as a complex number or a negative number.
There might be questions arising, like how will the user identify that the inputs given are wrong if the system shows an answer? or how to restrict the user from doing this mistake? The only answer is Unit testing. This method is the safest way to experiment with your codes without losing the original code.
Unit tests are performed on a separate code in a different file. But there is a way to write those filenames. The user could either write the name of the unit test file as the name of the code/unit +test
separated by an underscore or test+ name of the code/unit
separated by an underscore.
In this case, we can keep the file name for the unit test as square_area_test.py
and the above code file can be named as square_area.py
. To check out the unit testing code, it will be continued in the next blog.