Introduction to Python Unit Test
Unit testing means testing different blocks of code that provide some functionality, and it might result in errors in the future in case of any in proper inputs provided to the code. While creating software, the whole software is tested as a product in the end, but in the development phase of software coding, different code blocks are also tested with different possible values. Python provides an inbuilt module that can be used for unit testing the code. We just need to import the unit test module, and there are many inbuilt methods that can be used to carry out different tests.
We need to write different test cases and import our code file into our test case file for testing our code. Unit test modules support different oops concepts for testing the code.
- Test Fixture: Test fixture is used when we want to define the specific environment in which we need to carry out the test, like creating temporary databases.
- Test Cases: Test cases are certain conditions that are written to test the code, and it returns three results only.
- OK: It means all the test cases are successfully passed without any error, and our code is fine.
- Fail: It means the AssertionError exception is generated. It is generated when our code fails during the test case.
- Error: It means our test case has some error instead of AssertionError.
- Test Suite: Test suites are the collection of the test cases written in a file.
- Test Runner: The test runner is responsible for carrying out the test and write the result to the user.
Examples of Unit Test in Python
Following are the examples are given below:
def addition(x, y):
return x + y
def subtraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def division(x, y):
return x / y
This is a very basic program that does addition, subtraction, multiplication, and division. Save the above in file calc.py file. Now according to the rules of the unittest module of python, we have to create the test file for our code file. It can be named as test_calc.py or calc_test.py; both are good naming conventions. So we are saving our file with the test_calc.py name in the same folder as our original file.
self.assertEqual(calc.addition(5, 5), 10)
self.assertEqual(calc.addition(-3, 3), 0)
self.assertEqual(calc.addition(-5, -5), -10)
self.assertEqual(calc.subtraction(15, 5), 10)
self.assertEqual(calc.subtraction(-1, 2), -3)
self.assertEqual(calc.subtraction(-1, -1), 0)
self.assertEqual(calc.multiplication(20, 5), 100)
self.assertEqual(calc.multiplication(-2, 1), -2)
self.assertEqual(calc.multiplication(-1, -3), 3)
self.assertEqual(calc.division(10, 10), 1)
self.assertEqual(calc.division(-1, 1), -1)
self.assertEqual(calc.division(-2, -2), 1)
self.assertEqual(calc.division(6, 3), 2)
if __name__ == '__main__':
We have to write the above program in the test_calc.py file. We have written many test cases in this for all the 4 methods. We have imported unittest modules and our calc program. We have created TestCalc and inherited the TestCase from unittest module. It will provide us access to all the different testing methods from unittest. Now we have defined the method name. Method names should be the same as method names from the original name followed by test_. It is important to follow the naming convention; other methods will not run.
We have passed the self parameter like other methods. We have used the assertEqual method that checks whether the first parameter is equal to the second parameter. If it is equal, then it will return ok, which means the test case is successful.
As you can see, we have called the calc. add method and passed two parameters, as it takes two parameters as input and returns the addition of both parameters. We have passed 5,5, and passed 10 as the second output of the assertEqual method. Now it will compare with the sum of 5,5 with 10. It’s equal, so the output will be ok. Our test case was successful. Unit tests will be carried out for all the test cases, and the result will be returned accordingly, whether it is passed or failed.
“__name__” is the python’s built-in module, and it will return the name of the module. If another module uses our file, then the name will be assigned to the name variable. Currently, if we execute the __name__, it will return __main__ as the name of the module. So, if the condition becomes true and unittest, it will execute all the methods inside the main method, i.e. self.
Python provides an inbuilt module for unit testing our code. We don’t need to depend on any third-party tool. We can perform perfect unit testing using inbuilt modules only. It is best to practice to unit test our code before pushing it to the development server or production server. It reduces the effort in product level testing.
This is a guide to Python Unit Test. Here we discuss the Introduction and different test cases along with different examples and code implementation. You may also have a look at the following articles to learn more –