Hello All,
Welcome back to python article.
Myself chakrapani upadhyaya, a full stack engineer. In this article we will learn how to create the unit test cases in python.
Our Agenda?
- Lets create the math library which has two functions add and subtract
- Finally we will create the unit test case for both the method
Required Software:
Python to be installed in your machine.
Please refer article for Software Installation : Install Python
Lets Begin...
- Create folder in any drive( I will keep all the files in one drive)
My Drive is E:\Python\MyUnitTesting
- Shift+Right Click --> Open terminal or Open PowerShell window here
- Run the below command
code .
which help us to open the visual studio code in the same folder
- create the file called mathOperation.py
lets create the class MathOperation and add the two method Addition and Subtraction in it.
class MathOperation: def Addition(num1, num2): return num1 + num2 def Subtraction(num1, num2): return num1 - num2
- Next we will create the test file "math_test.py"
#importing the unittest library import unittest #import the math file import mathOperation #create the class file called MathOperationTest and
#inherit the unit test case class in it class MathOperationTest(unittest.TestCase): #test case for addition def test_addition(self): #creating the object for MathOperation class,
#access all the methods and properties in it mathObj = mathOperation.MathOperation() num1=10 num2=20 expectedResult=30 actualResult=mathObj.Addition(num1,num2) self.assertEqual(actualResult,expectedResult) #test case for addition def test_subtraction(self): mathObj = mathOperation.MathOperation() num1=50 num2=20 expectedResult=30 actualResult=mathObj.Subtraction(num1,num2) self.assertEqual(actualResult,expectedResult) if __name__=="__main__": unittest.main()
- Above we have created two test case one for Addition and another for Subtraction, now its time for run our test case
Open new terminal and run the below command
python .\math_test.py
Boom Here is the output..
Great, we have completed unit test case for our class library, now its time to run test case with error
Lets change the expectedResult = 20 in addition method and run the test case
def test_addition(self): #creating the object for MathOperation class, #access all the methods and properties in it mathObj = mathOperation.MathOperation() num1=10 num2=20 expectedResult=20 actualResult=mathObj.Addition(num1,num2) self.assertEqual(actualResult,expectedResult)
So, finally actual result is 30 but above we are expecting 20 which is wrong, then our test case should fail
lets run the test file
python .\math_test.py
Result is
Great, our test case is working as expected. That's it, we have completed our task. Lets understand something about unit testing
What is Unit Testing?
Unit testing is a software testing method by which individual units of source code are put under various tests to determine whether they are fit for use (Source). It determines and ascertains the quality of your code.
In our example, Python unittest module is used to test a unit of source code.
Why unittest?
There are many test runners available for Python. The one built into the Python standard library is called unittest.
Otheres are
- nose or nose2
- pytest
What is the Output for Python Unit Test?
Python unittest has 3 possible outcomes. Please find below:
- OK: If all test cases are passed, theen the output shows OK.
- Failure: If any of test cases failed and raised an AssertionError exception
- Error: If any exception other than AssertionError exception is raised.
That's it for this article, I hope you enjoyed, let me know in case any concerns.
See you in next article until then take care bye...