We can use the following frameworks provided by Python's inbuilt API for writing unit test cases in Python language:
1. unittest
Below are the steps to write a test case with the unittest module:
- Import the unitest module i.e. inbuilt module of python library
- Write a class that will inherit the unittest.TestCase class
- Test method must prefix by the string ‘test’
- Run the test class and test methods by unittest.main()
2. pytest
Below are the steps to write the test case with pytest module:
- Must start your class name with ‘test’ word
- Run your python file on the command line with pytest <pythonfile_name.py>
The pytest is the most recommended framework, since pytest also provides some extra features as compared to the unittest module. Some of these features are:
- Its fixtures provide a better approach for setup and teardown methods(@pytest.usefixtures)
- It provides parameterization of data for test methods (@mark.parameterize)
- It provides markers for grouping the test methods (@pytest.mark.<groupname>)
Note: There is no need to import the unittest module or inherit the unittest. TestCase is in pytest module.
Post a Comment