A pragmatic approach to QA and OPS

Python’s Unit Test Framework

Selenium is a browser interaction testing tool. It has very nice methods and built in functionality to test user interaction on the web. But it also has it’s limitations. We can’t test pre-conditions and post-conditions, check that expected and actual output comes same, check the state of application and report results. For doing so, we need to use the unit testing framework, along with Selenium.

Python provides us an inbuilt unit testing library that we can use to test using Selenium webdriver. This unit testing library is the unittest library provided by Python.


The unittest framework in Python , originally named PyUnit, is based on the JUnit unit testing framework in Java. If you want to know about the unit testing frameworks, and how it all started, you can go to this link. Kent Beck, was the first person who designed the first unit testing framework for Smalltalk language.

The unittest module can be used within the Python script to test various standard library modules, including the unittest module itself.

unittest lets us create and arrange our tests, by helping us breaking down the Python program/script in smaller, distinct and manageable blocks of code, or in simple words, it breaks down the program/script in to smaller components, which makes it better to understand and manage. The components are :

  1. Test Fixture :
  2. Test Case :
  3. Test Suite :
  4. Test Runner :
  5. Test Report

A unit testing framework like Python’s unittest framework, is based on principle of three A’s-

  • Arrange : Setting up the pre-requisites for the test
  • Act : Exercise the main functionality of test
  • Assert : Checks whether the outcome is expected or not.

Using the TestCase Class to Design A Test Case

Using Python’s unit test framework, unittest, we can create a test, or a group of tests, by inheriting the TestCase class. We can then add each test as a method of that class.

Basically , what we want to do in the test is this

  •  Add a test fixture– that is the setUp( ) and tearDown( ) methods to handle the creation and destroying any objects or conditions created for use in the test.
  • Adding an Assert to check the out come : In between the test fixtures, we need to add any one of the variations of the Assert method, to check the outcome of the test. This may include using assertEqual( ) to check for a condition, assertTrue( ) to verify a condition or assertRaises( ) to verify that an exception gets raised in case a test fails.

Let us see a simple example of a simple python script using the unittest framework.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: