Selenium Test case using Data Driven Testing in Python

In this tutorial, We are going to create a Data driven framework in Selenium Python from start to end.

Before that let’s understand what is Data Driven Testing? and Why it is required.

 

What is Data Driven Testing?

In data-driven testing approach, we can use a single test to verify different sets of test cases or test data by driving the test with input and expected values from an external data source instead of using the hard-coded values every time a test is run.

Why it is Required?

Its very useful when we have similar tests that consist of the same steps but differ in the input data and expected value or the application state.

We need to install DDT in python to work with Data driven Testing

Installing DDT

We can download and install ddt using the following command line:

 

 

PS. – Assuming that you have unittest,selenium and nose installed. If not go here.

If you are new to the Selenium Python – Checkout out below post.

  1. Step by Step Selenium With Python – Part 1
  2. Getting Started with Selenium Python – Part 2 (Unittest module)
  3. Getting Started with Selenium Testcases in Python – Part 3
  4. Getting Started with Selenium Test cases in Python – Part 4

Steps to Create the DDT using unittest , nose with external data sources(Excel):

1.  Use the @ddt decorator for the test class.
2.  Use the @data decorator on the data-driven test methods.
3.  Use the @unpack decorator, which unpacks tuples or lists into multiple arguments.

Project Structure:

Importing ddt in the  test

 

 

Let’s write code for the Searching a Text on the “http://demo.magentocommerce.com” site and getting expected results. I have written a search test, which accepts a pair of arguments for different search value and expected value.

 

Running the test case with Nose

DDT will generate new test methods giving them meaningful names by converting the data values to valid Python identifier.
Final Code Here

Check out my other tutorials in the API Testing and Local Testing below

The Definitive Guide to Do API Testing in 2019

How to do Local testing and what is it?

4 thoughts on “Selenium Test case using Data Driven Testing in Python”

  1. @promode
    Very good solution . Really helpful.
    I have 2 questions:
    1- How to manage the ddt to retrive rows only related to the test case based on TestName column ?
    2- in line 25
    @data(*getData(“TestData.xlsx”))
    The path for the data files is not defined which is awesome for CI, but in mu case Selenium is throwing error because it cant find the excel data file

Leave a Comment


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

3 Shares
Share3
Tweet
Pin