Introduction:
In this post, we are going to describe the different frameworks used to run unit test for python codes. This is not a comprehensive article on unit testing in general, but the aim is to explore several frameworks for python code unit testing. This post is intended for beginner and amateur level python developers who are interested to explore python unit testing frameworks. First lets explore the different testing scenarios one encounters.
Different testing scenarios:
There are mainly two types of testing in software, functional and non-functional. We have taken these common test types from softwaretesting site.
Functional Testing types include:
- Unit Testing
- Integration Testing
- System Testing
- Sanity Testing
- Smoke Testing
- Interface Testing
- Regression Testing
- Beta/Acceptance Testing
Non-functional Testing types include:
- Performance Testing
- Load Testing
- Stress Testing
- Volume Testing
- Security Testing
- Compatibility Testing
- Install Testing
- Recovery Testing
- Reliability Testing
- Usability Testing
- Compliance Testing
- Localization Testing
We are going to explore unit testing for python codes in this post.
What is unit testing?
Unit testing is the first level of testing in software development life cycle or SDLC. Normally unit testing is carried out for each independent module to test their functional accuracy and logics. Therefore, it comes at the first step of the testing.
Normally unit testing for a individual component is carried out by its developer only. Developers write unit tests and then according to test results codes are often modified, upgraded or re-written. Now that you have the idea about what is a unit testing; we will dive into python unit testing frameworks.
Python unittesting frameworks:
Unittest module: basics and details:
First framework which we will look into is python unittest which is one of the most simple and easy to work with frameworks. Being a python framework, it supports the object oriented setup for python.
unittest provides a base class TestCase, on the top of which one builds aggregate of test cases.
The general format of unittesting class is:
import unittest
class test_you_write(TestCase):
def SetUp(self):
#random setup codes
def test_goodname(self):
#self.assertEqual(function_output, output_expected)
if __name__ == '__main__':
unittest.main()
Import points which are not mentioned as trivial here are:
(1) it is customary to write the name of your test functions begining with "test_". if you don't obey the nomenclature, those functions will be ignored when the main function runs to test the functions.
(2) The last format is most important, as it is again a customary pattern to run tests like that.
(3) The normal pattern of unit test functions are to check whether the output of developer written function is equal to the expected output. The way we verify that is using the assertEqual function. There are a bunch of similar functions for different types of checks.
The output of this program is normally
i.e. if all the tests pass. If some of the tests fail then it will say that which test failed and at what point.
Now, lets discuss a bit more organization features of the framework. Like in normal OOP structures, some variables tend to get used in each test cases as well as some codes can get repeated. For these, it is a general notion to create those variables in a init like function inside the class first. In case of TestCase, this init like function is setUp. In setUp we can initiate those variables which we use in all the test cases. There is also a destructing pair of setUp named tearDown() which is used to close the environment variables created or instances opened during setUp(). It is important to use tearDown after completing your experiments using setUp.
An example from the unittest official documentation is as follows:
There is also a construction named TestSuite class. This works as a functional setup rather than being an OOP setting. After instantiating a object of TestSuite(); one can add tests in functions using addTest(function(parameter)) setup. Finally one can execute it again using main().
For knowing the greater details of different assert*() functions as well as features as sort of how to skip tests known for failure and others, please follow the official documentation for instructions.
Comments
Post a Comment