Chapter 7 Data-Driven Testing
7.1 Creating a data-driven test in Python using DDT
import unittest
from ddt import ddt,data,unpack
from selenium import webdriver
@ddt
class JianShu(unittest.TestCase):
def setUp(self):
print("here is setup")
self.driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
self.driver.implicitly_wait(30)
self.driver.maximize_window()
# navigate to the BMI Calculator page
self.driver.get("https://www.jianshu.com")
@data("abc","def")
#@unpack
def test_SearchWeb(self,SearchName):
print("here is test case")
driver = self.driver
search_area = driver.find_element_by_id("q")
search_area.clear()
search_area.send_keys(SearchName)
button = driver.find_element_by_class_name("search-btn")
button.click()
def tearDown(self):
print("here is tearDown")
self.driver.quit()
if __name__ == '__main__':
unittest.main()
7.2 Reading test data from a CSV file using unittest
import csv,unittest
from ddt import ddt,data,unpack
from selenium import webdriver
def get_data(file_name):
rows = []
data_file = open(file_name,"rt")
reader = csv.reader(data_file)
next(reader,None)
for row in reader:
rows.append(row)
return rows
@ddt
class JianShu(unittest.TestCase):
def setUp(self):
print("here is setup")
self.driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
self.driver.implicitly_wait(30)
self.driver.maximize_window()
# navigate to the BMI Calculator page
self.driver.get("https://www.jianshu.com")
@data(*get_data("testdata.csv"))
#@unpack
def test_SearchWeb(self,SearchName):
print("here is test case")
driver = self.driver
search_area = driver.find_element_by_id("q")
search_area.clear()
search_area.send_keys(SearchName)
button = driver.find_element_by_class_name("search-btn")
button.click()
def tearDown(self):
print("here is tearDown")
self.driver.quit()
if __name__ == '__main__':
unittest.main()
网友评论