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.
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