Python UNITTEST Introduction














































Python UNITTEST Introduction



CODE TESTING INTRODUCTION IN PYTHON



One of the important question after writing the code, should I submit a pull request or wait
for my teammates to review it? All these things are necessary but the more important thing is
"is your code is working as intended"? The solution to this problem is unit testing of code.

Unit Testing is the basic level of software testing where individual units of source code are
put under the various test to determine whether they are fit for use or not. This is used to
validate that the software is working as it is intended or not.
The unit test test framework is python xUnit style framework.

Unit Testing makes your code future proof since you anticipate the cases where your code could
potentially fail or produce a bug. Though you cannot predict all the cases, you still address
most of them. The best way to write unit tests for your code is to first start with a smallest
testable unit your code could possibly have, then move on to other units and see how that
smallest unit interacts with other units, this way you could build up a comprehensive unit
test for your applications.

The basic Unittest code is:-
import unittest
class Sample(unittest.TestCase):
def sample_test(self):
self.assertTrue(
True)

if __name__ == '__main__':
unittest.main()

The output is:


Here, in the output, the "." on the first line of output means that a test passed.

Without adding some extra words let start with example by writing a code of area of rectangle:-
def area(l,b):
return l*b
if __name__ == '__main__':
a1=area(
3,4)
print(
"Area 1: ",a1)
a2=area(
-2,4)
print(
"Area 2: ",a2)

Here Output is:


The Output here shows the importance of Unittest in
the code.
-Area 2 is shown negative here but it is technically
wrong as the area can't be negative.

Unit tests are usually written as a separate
code in a different file, and there could be the
different naming convention for this:
-name of the code/unit+test
-test+name of the code/unit
First,
we created a Python file with name area.py which calculate the area and second with the
name test_area.py, which will have the unit testing code.

Passing a Test


area.py file contains:-
def area(l,b):
return l*b

test_area.py file contains:-
from teach import *
import unittest
class Testarea(unittest.TestCase):
def test_area(self):
self.assertAlmostEqual(area(
2,3),6)
self.assertAlmostEqual(area(
0,0),0)
self.assertAlmostEqual(area(
2.5,3.5),8.75)

Ok, Now here the Testarea class inherits the "unittest" module, and in this class, you would
define various methods that you want your unit test should check with your function 'area'.The
the first function you will define is test_area, which will check whether the output your 'area'
gives is equal to what you expect. To achieve this, you will make use of the assertAlmostEqual
method.
Let's run the above code:-


Great, so the test run successfully and return ok, which means the 'area function is working
properly.

                                             Failing a Test

Now we will see what happens when the assertAlmostEqual method fails.
There little change in the test_area.py file in last method and changed to 0.
from teach import *
import unittest
class Testarea(unittest.TestCase):
def test_area(self):
self.assertAlmostEqual(area(
2,3),6)
self.assertAlmostEqual(area(
0,0),0)
self.assertAlmostEqual(area(
2.5,3.5),0)

Let's see the change in the output section:-


Well, from the output we can observe that the last assert statement resulted in an
"AssertionError", hence a unit test failure. Python unit test modules show you the reason for
the failure, along with the number of failures your code has.

What to do when the test has failed

"A passing test means the function is behaving according to what's expected from it. However,
 a
failing test means spending a little more time to fix the issue, as it will help to
better understand the code and save time in the long run".So, work on the code and fix it.


Happy Coding...

Comments

  • Deepanshu
    9-Sep-2020 12:25:26 AM
    Great Content !!