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
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.
- Step by Step Selenium With Python – Part 1
- Getting Started with Selenium Python – Part 2 (Unittest module)
- Getting Started with Selenium Testcases in Python – Part 3
- 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.
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?
6 thoughts on “Selenium Test case using Data Driven Testing in Python”
Downloaded the final code using the link provided for python selenium DDT framework. Working a Charm Thanks 🙂
Glad you liked it, Please share 🤩
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
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
You probably though to create a reference link this way:
PS. – Assuming that you have unittest,selenium and nose installed. If not go here
unable to download the DDT selenium. If any body have it can you share it to email@example.com