最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Selenium 之?dāng)?shù)據(jù)驅(qū)動(dòng)測(cè)試的實(shí)現(xiàn)

 更新時(shí)間:2019年08月01日 14:39:27   作者:菜鳥可米  
這篇文章主要介紹了Python Selenium 之?dāng)?shù)據(jù)驅(qū)動(dòng)測(cè)試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

數(shù)據(jù)驅(qū)動(dòng)模式的測(cè)試好處相比普通模式的測(cè)試就顯而易見了吧!使用數(shù)據(jù)驅(qū)動(dòng)的模式,可以根據(jù)業(yè)務(wù)分解測(cè)試數(shù)據(jù),只需定義變量,使用外部或者自定義的數(shù)據(jù)使其參數(shù)化,從而避免了使用之前測(cè)試腳本中固定的數(shù)據(jù)??梢詫y(cè)試腳本與測(cè)試數(shù)據(jù)分離,使得測(cè)試腳本在不同數(shù)據(jù)集合下高度復(fù)用。不僅可以增加復(fù)雜條件場(chǎng)景的測(cè)試覆蓋,還可以極大減少測(cè)試腳本的編寫與維護(hù)工作。

下面將使用Python下的數(shù)據(jù)驅(qū)動(dòng)模式(ddt)庫(kù),結(jié)合unittest庫(kù)以數(shù)據(jù)驅(qū)動(dòng)模式創(chuàng)建百度搜索的測(cè)試。

ddt庫(kù)包含一組類和方法用于實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)測(cè)試??梢詫y(cè)試中的變量進(jìn)行參數(shù)化。

可以通過python自帶的pip命令進(jìn)行下載并安裝:pip install ddt . 更多關(guān)于ddt的信息可以參考:

https://pypi.org/project/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é)果如下:

使用外部數(shù)據(jù)的數(shù)據(jù)驅(qū)動(dòng)測(cè)試

如果外部已經(jīng)存在了需要的測(cè)試數(shù)據(jù),如一個(gè)文本文件、電子表格或者數(shù)據(jù)庫(kù),那也可以用ddt來直接獲取數(shù)據(jù)并傳入測(cè)試方法進(jìn)行測(cè)試。

下面將借助外部的CSV(逗號(hào)分隔值)文件和EXCLE表格數(shù)據(jù)來實(shí)現(xiàn)ddt。

通過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庫(kù)去讀取文件并返回一行數(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é)果也同上~

通過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庫(kù)去讀取文件并返回?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ù)庫(kù)的庫(kù)表中獲取數(shù)據(jù),同樣也需要一個(gè)get_data()方法,并且通過DB相關(guān)的庫(kù)來連接數(shù)據(jù)庫(kù)、SQL查詢來獲取測(cè)試數(shù)據(jù)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Python實(shí)現(xiàn)格斗小游戲的示例代碼

    基于Python實(shí)現(xiàn)格斗小游戲的示例代碼

    格斗游戲,曾經(jīng)是街機(jī)廳里最火爆的游戲之一,甚至可以把“之一”去掉,那個(gè)年代的格斗游戲就是街機(jī)游戲的王。本文就來用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的格斗游戲,感興趣的可以了解一下
    2023-03-03
  • 利用Matlab繪制各類特殊圖形的實(shí)例代碼

    利用Matlab繪制各類特殊圖形的實(shí)例代碼

    作為一個(gè)功能強(qiáng)大的工具軟件,Matlab具有很強(qiáng)的圖形處理功能,提供了大量的二維、三 維圖形函數(shù),這篇文章主要給大家介紹了關(guān)于如何利用Matlab繪制各類特殊圖形的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • 在Pandas中給多層索引降級(jí)的方法

    在Pandas中給多層索引降級(jí)的方法

    今天小編就為大家分享一篇在Pandas中給多層索引降級(jí)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口

    Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口

    這篇文章主要介紹了Python如何設(shè)置指定窗口為前臺(tái)活動(dòng)窗口,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 如何基于Python實(shí)現(xiàn)word文檔重新排版

    如何基于Python實(shí)現(xiàn)word文檔重新排版

    這篇文章主要介紹了如何基于Python實(shí)現(xiàn)word文檔重新排版,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 淺談?dòng)肞ython實(shí)現(xiàn)一個(gè)大數(shù)據(jù)搜索引擎

    淺談?dòng)肞ython實(shí)現(xiàn)一個(gè)大數(shù)據(jù)搜索引擎

    這篇文章主要介紹了淺談?dòng)肞ython實(shí)現(xiàn)一個(gè)大數(shù)據(jù)搜索引擎,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Python之lxml安裝失敗的解決

    Python之lxml安裝失敗的解決

    這篇文章主要介紹了Python之lxml安裝失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python?redis模塊的使用教程指南

    Python?redis模塊的使用教程指南

    這篇文章主要為大家詳細(xì)介紹了Python?redis模塊的使用教程指南的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2022-10-10
  • Python爬蟲requests庫(kù)多種用法實(shí)例

    Python爬蟲requests庫(kù)多種用法實(shí)例

    這篇文章主要介紹了Python爬蟲requests庫(kù)多種用法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • python中flatten()函數(shù)用法詳解

    python中flatten()函數(shù)用法詳解

    本文主要介紹了python中flatten()函數(shù)用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論

丁青县| 偃师市| 宜兴市| 乐平市| 汾阳市| 宁明县| 桓台县| 龙口市| 扶风县| 鄢陵县| 张掖市| 莱芜市| 夹江县| 怀宁县| 小金县| 霍林郭勒市| 肥城市| 探索| 明水县| 巴青县| 响水县| 会东县| 青神县| 涞水县| 顺平县| 彩票| 永定县| 蓬安县| 沂水县| 青田县| 澄迈县| 佛坪县| 花莲市| 沽源县| SHOW| 久治县| 瑞丽市| 元江| 澎湖县| 神农架林区| 云龙县|