PyUnit Introduction
Python’s PyUnit is commonly referred to as the unittest module. It is modeled after Java’s JUnit unit testing framework in Java. Kent Beck and Erich Gamma developed it.
JUnit
JUnit is an open-source Unit Testing framework for writing and running repeatable unit tests in Java. It is an instance of the xUnit architecture for unit testing frameworks. Most Java developers use it as a unit testing tool.
When you write your test scripts, you must structure them with the help of a framework. You can do this using unittest, the unit test framework.
Import unittest module
You can import the unittest module into your Python script using the following command.
import unittest
Create a Test Case
You can create a test case by deriving the test from the class unittest.TestCase
class MyTestCase(unittest.TestCase):
Â
Basic Structure
The basic structure of the PyUnit test is as follows:
- setUp()
- test_method()Â
- tearDown()
setUp()
- In the setUp() method, you can write code to prepare the system for the test environment, such as initializing variables and instantiating a driver object.
test_ Methods
The test_ methods contain the code where the test code is written. These functions are written using the test_ prefix. You can have multiple test methods. Each test method can be independent and test application logic or flow.
For example:
- test_method()
- test_login()
tearDown()
You can write code to clean up the environment using the tearDown() method. This method executes cleanup code, such as closing browser instances, database connections, and file handles.
You can execute the tests by calling the unittest.main() method.
More information on unittest can be found at: