-
Notifications
You must be signed in to change notification settings - Fork 2
20. Data Driven Testing using @ddt and Excel data source
NaveenS edited this page Sep 7, 2019
·
5 revisions
-
Create a excel data source

-
Create readexceldata.py file and have a function to read the data from excel using xlrd module
import xlrd
def getData():
data=[]
wb = xlrd.open_workbook("Credential.xlsx")
sheet = wb.sheet_by_index(0)
for i in range(1, sheet.nrows):
data.append((sheet.cell_value(i, 0),sheet.cell_value(i, 1)))
return data
- Create a test class and decorate with @ddt
- Use the @data decorator on the test methods
- Use the @unpack decorator, which unpacks tuples or lists into multiple arguments
import unittest
from selenium import webdriver
from ddt import ddt, data,unpack
from readexceldata import getData
@ddt
class TestClass(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome(r'.\chromedriver.exe')
cls.driver.maximize_window()
cls.driver.get("http://zero.webappsecurity.com/")
cls.driver.find_element_by_id("signin_button").click()
@data(*getData())
@unpack
def test_login_with_invalid_credential(self,uname,pword):
self.driver.find_element_by_id("user_login").send_keys(uname)
self.driver.find_element_by_id("user_password").send_keys(pword)
self.driver.find_element_by_name("submit").click()
assert "Zero - Log in" == self.driver.title
@classmethod
def tearDownClass(cls):
driver.quit()
if __name__=='__main__':
unittest.main()
For CSV data source reading
Refer datadriven_csv module