一综慎、數(shù)據(jù)驅(qū)動(dòng)
模式的測(cè)試好處相比普通模式的測(cè)試就顯而易見了吧窗宦!使用數(shù)據(jù)驅(qū)動(dòng)的模式湖雹,可以根據(jù)業(yè)務(wù)分解測(cè)試數(shù)據(jù)咏闪,只需定義變量,使用外部或者自定義的數(shù)據(jù)使其參數(shù)化摔吏,從而避免了使用之前測(cè)試腳本中固定的數(shù)據(jù)鸽嫂。可以將測(cè)試腳本與測(cè)試數(shù)據(jù)分離征讲,使得測(cè)試腳本在不同數(shù)據(jù)集合下高度復(fù)用据某。不僅可以增加復(fù)雜條件場(chǎng)景的測(cè)試覆蓋,還可以極大減少測(cè)試腳本的編寫與維護(hù)工作诗箍。
下面將使用Python下的數(shù)據(jù)驅(qū)動(dòng)模式(ddt)庫癣籽,結(jié)合unittest庫以數(shù)據(jù)驅(qū)動(dòng)模式創(chuàng)建百度搜索的測(cè)試。
ddt庫包含一組類和方法用于實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)測(cè)試扳还〔疟埽可以將測(cè)試中的變量進(jìn)行參數(shù)化。
可以通過python自帶的pip命令進(jìn)行下載并安裝:pip install ddt .
二氨距、一個(gè)簡(jiǎn)單的數(shù)據(jù)驅(qū)動(dòng)測(cè)試
為了創(chuàng)建數(shù)據(jù)驅(qū)動(dòng)測(cè)試桑逝,需要在測(cè)試類上使用@ddt裝飾符,在測(cè)試方法上使用@data裝飾符俏让。@data裝飾符把參數(shù)當(dāng)作測(cè)試數(shù)據(jù)楞遏,參數(shù)可以是單個(gè)值、列表首昔、元組寡喝、字典。對(duì)于列表勒奇,需要用@unpack裝飾符把元組和列表解析成多個(gè)參數(shù)预鬓。
下面實(shí)現(xiàn)百度搜索測(cè)試,傳入搜索關(guān)鍵詞和期望結(jié)果赊颠,代碼如下:
import unittest
from selenium import webdriver
from ddt import ddt, data, unpack
@ddt
class SearchDDT(unittest.TestCase):
? ? '''docstring for SearchDDT'''
? ? def setUp(self):
? ? ? ? self.driver = webdriver.Chrome()
? ? ? ? self.driver.implicitly_wait(30)
? ? ? ? self.driver.maximize_window()
? ? ? ? self.driver.get("https://www.baidu.com")
? ? # specify test data using @data decorator
? ? @data(('python', 'PyPI'))
? ? @unpack
? ? def test_search(self, search_value, expected_result):
? ? ? ? search_text = self.driver.find_element_by_id('kw')
? ? ? ? search_text.clear()
? ? ? ? search_text.send_keys(search_value)
? ? ? ? search_button = self.driver.find_element_by_id('su')
? ? ? ? search_button.click()
? ? ? ? tag = self.driver.find_element_by_link_text("PyPI").text
? ? ? ? self.assertEqual(expected_result, tag)
? ? def tearDown(self):
? ? ? ? self.driver.quit()
if __name__ == '__main__':
? ? unittest.main(verbosity=2)
在test_search()方法中格二,search_value與expected_result兩個(gè)參數(shù)用來接收元組解析的數(shù)據(jù)劈彪。當(dāng)運(yùn)行腳本時(shí),ddt把測(cè)試數(shù)據(jù)轉(zhuǎn)換為有效的python標(biāo)識(shí)符顶猜,生成名稱為更有意義的測(cè)試方法沧奴。結(jié)果如下:
1、使用外部數(shù)據(jù)的數(shù)據(jù)驅(qū)動(dòng)測(cè)試
如果外部已經(jīng)存在了需要的測(cè)試數(shù)據(jù)长窄,如一個(gè)文本文件滔吠、電子表格或者數(shù)據(jù)庫,那也可以用ddt來直接獲取數(shù)據(jù)并傳入測(cè)試方法進(jìn)行測(cè)試挠日。
下面將借助外部的CSV(逗號(hào)分隔值)文件和EXCLE表格數(shù)據(jù)來實(shí)現(xiàn)ddt疮绷。
2、通過CSV獲取數(shù)據(jù)
同上在@data裝飾符使用解析外部的CSV(testdata.csv)來作為測(cè)試數(shù)據(jù)(代替之前的測(cè)試數(shù)據(jù))嚣潜。其中數(shù)據(jù)如下
接下來矗愧,先要?jiǎng)?chuàng)建一個(gè)get_data()方法,其中包括路徑(這里默認(rèn)使用當(dāng)前路徑)郑原、CSV文件名唉韭。調(diào)用CSV庫去讀取文件并返回一行數(shù)據(jù)川抡。再使用@ddt及@data實(shí)現(xiàn)外部數(shù)據(jù)驅(qū)動(dòng)測(cè)試百度搜索活烙,代碼如下:
import csv, unittest
from selenium import webdriver
from ddt import ddt, data, unpack
def get_data(file_name):
? ? # create an empty list to store rows
? ? rows = []
? ? # open the CSV file
? ? data_file = open(file_name, "r")
? ? # create a CSV Reader from CSV file
? ? reader = csv.reader(data_file)
? ? # skip the headers
? ? next(reader, None)
? ? # add rows from reader to list
? ? for row in reader:
? ? ? ? rows.append(row)
? ? return rows
@ddt
class SearchCSVDDT(unittest.TestCase):
? ? def setUp(self):
? ? ? ? self.driver = webdriver.Chrome()
? ? ? ? self.driver.implicitly_wait(30)
? ? ? ? self.driver.maximize_window()
? ? ? ? self.driver.get("https://www.baidu.com")
? ? # get test data from specified csv file by using the get_data funcion
? ? @data(*get_data('testdata.csv'))
? ? @unpack
? ? def test_search(self, search_value, expected_result):
? ? ? ? search_text = self.driver.find_element_by_id('kw')
? ? ? ? search_text.clear()
? ? ? ? search_text.send_keys(search_value)
? ? ? ? search_button = self.driver.find_element_by_id('su')
? ? ? ? search_button.click()
? ? ? ? tag = self.driver.find_element_by_link_text("PyPI").text
? ? ? ? self.assertEqual(expected_result, tag)
? ? def tearDown(self):
? ? ? ? self.driver.quit()
if __name__ == '__main__':
? ? unittest.main(verbosity=2)
測(cè)試執(zhí)行時(shí),@data將調(diào)用get_data()方法讀取外部數(shù)據(jù)文件籽慢,并將數(shù)據(jù)逐行返回給@data酸役。執(zhí)行的結(jié)果也同上
3住诸、通過Excel獲取數(shù)據(jù)
測(cè)試中經(jīng)常用Excle存放測(cè)試數(shù)據(jù),同上在也可以使用@data裝飾符來解析外部的CSV(testdata.csv)來作為測(cè)試數(shù)據(jù)(代替之前的測(cè)試數(shù)據(jù))涣澡。其中數(shù)據(jù)如下:
接下來贱呐,先要?jiǎng)?chuàng)建一個(gè)get_data()方法,其中包括路徑(這里默認(rèn)使用當(dāng)前路徑)入桂、EXCEL文件名奄薇。調(diào)用xlrd庫去讀取文件并返回?cái)?shù)據(jù)。再使用@ddt及@data實(shí)現(xiàn)外部數(shù)據(jù)驅(qū)動(dòng)測(cè)試百度搜索抗愁,代碼如下:
import xlrd, unittest
from selenium import webdriver
from ddt import ddt, data, unpack
def get_data(file_name):
? ? # create an empty list to store rows
? ? rows = []
? ? # open the CSV file
? ? book = xlrd.open_workbook(file_name)
? ? # get the frist sheet
? ? sheet = book.sheet_by_index(0)
? ? # iterate through the sheet and get data from rows in list
? ? for row_idx in range(1, sheet.nrows):? #iterate 1 to maxrows
? ? ? ? rows.append(list(sheet.row_values(row_idx, 0, sheet.ncols)))
? ? return rows
@ddt
class SearchEXCLEDDT(unittest.TestCase):
? ? def setUp(self):
? ? ? ? self.driver = webdriver.Chrome()
? ? ? ? self.driver.implicitly_wait(30)
? ? ? ? self.driver.maximize_window()
? ? ? ? self.driver.get("https://www.baidu.com")
? ? # get test data from specified excle spreadsheet by using the get_data funcion
? ? @data(*get_data('TestData.xlsx'))
? ? @unpack
? ? def test_search(self, search_value, expected_result):
? ? ? ? search_text = self.driver.find_element_by_id('kw')
? ? ? ? search_text.clear()
? ? ? ? search_text.send_keys(search_value)
? ? ? ? search_button = self.driver.find_element_by_id('su')
? ? ? ? search_button.click()
? ? ? ? tag = self.driver.find_element_by_link_text("PyPI").text
? ? ? ? self.assertEqual(expected_result, tag)
? ? def tearDown(self):
? ? ? ? self.driver.quit()
if __name__ == '__main__':
? ? unittest.main(verbosity=2)
與上面讀取CVS文件一樣馁蒂,測(cè)試執(zhí)行時(shí),@data將調(diào)用get_data()方法讀取外部數(shù)據(jù)文件蜘腌,并將數(shù)據(jù)逐行返回給@data沫屡。執(zhí)行的結(jié)果也同上~
如果想從數(shù)據(jù)庫的庫表中獲取數(shù)據(jù),同樣也需要一個(gè)get_data()方法撮珠,并且通過DB相關(guān)的庫來連接數(shù)據(jù)庫沮脖、SQL查詢來獲取測(cè)試數(shù)據(jù)。以上內(nèi)容希望對(duì)你有幫助,有被幫助到的朋友歡迎點(diǎn)贊勺届,評(píng)論绷柒。如果對(duì)軟件測(cè)試、接口測(cè)試涮因、自動(dòng)化測(cè)試、面試經(jīng)驗(yàn)交流伺绽。感興趣可以加軟件測(cè)試交流:829792258养泡,還會(huì)有同行一起技術(shù)交流。