Course Content
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

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.

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):

 

Import PyUnit module EclipseIDE

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.

More information on unittest can be found at:

Join the conversation