Python自動(dòng)化測(cè)試的五種模型
1、前言
在自動(dòng)化測(cè)試中,我們往往將自動(dòng)化腳本都?xì)w納屬于哪種框架模型,比如關(guān)鍵字驅(qū)動(dòng)模型等。
本篇將列舉實(shí)際自動(dòng)化測(cè)試中,Python 自動(dòng)化測(cè)試的五種模型:線性模型、模塊化驅(qū)動(dòng)模型、數(shù)據(jù)驅(qū)動(dòng)模型、關(guān)鍵字驅(qū)動(dòng)模型、行為驅(qū)動(dòng)模型。
2、線性模型
通過(guò)錄制或編寫(xiě)腳本,一個(gè)腳本完成一個(gè)場(chǎng)景(一組完整功能操作),通過(guò)對(duì)腳本的回放進(jìn)行自動(dòng)化測(cè)試。
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
time.sleep(1)
driver.find_element_by_id('kw').send_keys('自動(dòng)化測(cè)試')
time.sleep(1)
driver.find_element_by_id('su').click()
time.sleep(1)
driver.quit()
3、模塊化驅(qū)動(dòng)模型
將腳本中重復(fù)可復(fù)用的部分拿出來(lái)寫(xiě)成一個(gè)公共的模塊,需要的時(shí)候就調(diào)用它,這樣可以大幅提高測(cè)試人員編寫(xiě)腳本的效率。
框架目錄:
config 存放配置文件。
例如 base_data.json 文件,存放測(cè)試地址。
{
"url": "https://www.baidu.com/"
}
data 存放測(cè)試數(shù)據(jù)。
drivers 存放瀏覽器驅(qū)動(dòng)文件。
report 存放執(zhí)行完成后的測(cè)試報(bào)告。
test 存放測(cè)試用例。
case 測(cè)試用例步驟。
例如 testSearch.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
import time
import os
import unittest
from selenium import webdriver
from AutomatedTestModel.ModularDriverModel.utils.ReadConfig import ReadConfig
from AutomatedTestModel.ModularDriverModel.test.pages.searchPage import SearchPage
class TestSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
def tearDown(self):
self.driver.quit()
def get_url(self):
current_path = os.path.abspath((os.path.dirname(__file__)))
data = ReadConfig().read_json(current_path + "/../../config/base_data.json")
return data['url']
def test_search(self):
url = self.get_url()
self.driver.get(url)
time.sleep(1)
search = SearchPage(self.driver)
search.search('自動(dòng)化測(cè)試')
if __name__ == '__main__':
unittest.main()
common 存放公共的方法等。
pages 存放頁(yè)面元素與頁(yè)面操作。
例如 searchPage.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
import time
class SearchPage:
def __init__(self, driver):
self.driver = driver
def search_element(self):
self.kw = self.driver.find_element_by_id('kw')
self.su = self.driver.find_element_by_id('su')
def search(self, data):
self.search_element()
self.kw.send_keys(data)
time.sleep(1)
self.su.click()
runner 存放運(yùn)行腳本。
例如 main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
import os
import time
import unittest
from AutomatedTestModel.ModularDriverModel.utils.HwTestReport import HTMLTestReport
class Main:
def get_all_case(self):
current_path = os.path.abspath(os.path.dirname(__file__))
case_path = current_path + '/../case/'
discover = unittest.defaultTestLoader.discover(case_path, pattern="test*.py")
print(discover)
return discover
def set_report(self, all_case, report_path=None):
if report_path is None:
current_path = os.path.abspath(os.path.dirname(__file__))
report_path = current_path + '/../../report/'
else:
report_path = report_path
# 獲取當(dāng)前時(shí)間
now = time.strftime('%Y{y}%m{m}%d5gk0f71da%H{h}%M{M}%S{s}').format(y="年", m="月", d="日", h="時(shí)", M="分", s="秒")
# 標(biāo)題
title = u"搜索測(cè)試"
# 設(shè)置報(bào)告存放路徑和命名
report_abspath = os.path.join(report_path, title + now + ".html")
# 測(cè)試報(bào)告寫(xiě)入
with open(report_abspath, 'wb') as report:
runner = HTMLTestReport(stream=report,
verbosity=2,
images=True,
title=title,
tester='Meng')
runner.run(all_case)
def run_case(self, report_path=None):
all_case = self.get_all_case()
self.set_report(all_case, report_path)
if __name__ == '__main__':
Main().run_case()
utils 存放公共方法。
例如導(dǎo)出報(bào)告樣式、讀取配置文件等。
run.py 運(yùn)行腳本。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
from AutomatedTestModel.ModularDriverModel.test.runner.main import Main
if __name__ == '__main__':
Main().run_case()
運(yùn)行后的測(cè)試報(bào)告。
4、數(shù)據(jù)驅(qū)動(dòng)模型
該模型會(huì)根據(jù)數(shù)據(jù)的變化而引起測(cè)試結(jié)果的改變,這顯然是一個(gè)非常高級(jí)的概念和想法。簡(jiǎn)單地說(shuō),該模型是一種數(shù)據(jù)的參數(shù)化呈現(xiàn),即通過(guò)輸入不同的參數(shù)來(lái)驅(qū)動(dòng)程序執(zhí)行,輸出不同的測(cè)試結(jié)果。
框架目錄:
case 存放測(cè)試用例步驟。
common 存放公共的方法等。
如讀取 Excel 方法、生成報(bào)告等樣式。
data 存放測(cè)試數(shù)據(jù)與預(yù)期結(jié)果。
report 存放執(zhí)行完成后的測(cè)試報(bào)告。
打開(kāi)報(bào)告效果。
RunMain.py 運(yùn)行腳本。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
import os, time, unittest
from AutomatedTestModel.DataDrivenModeling.common.HwTestReport import HTMLTestReport
class RunMain:
def get_all_case(self):
case_path = os.getcwd()
discover = unittest.defaultTestLoader.discover(case_path,
pattern="Test*.py")
print(discover)
return discover
def set_report(self, all_case, report_path=None):
if report_path is None:
current_path = os.path.abspath(os.path.dirname(__file__))
report_path = current_path + '/report/'
else:
report_path = report_path
# 獲取當(dāng)前時(shí)間
now = time.strftime('%Y{y}%m{m}%d5gk0f71da%H{h}%M{M}%S{s}').format(y="年", m="月", d="日", h="時(shí)", M="分", s="秒")
# 標(biāo)題
title = u"搜索測(cè)試"
# 設(shè)置報(bào)告存放路徑和命名
report_abspath = os.path.join(report_path, title + now + ".html")
# 測(cè)試報(bào)告寫(xiě)入
with open(report_abspath, 'wb') as report:
runner = HTMLTestReport(stream=report,
verbosity=2,
images=True,
title=title,
tester='Meng')
runner.run(all_case)
def run_case(self, report_path=None):
all_case = self.get_all_case()
self.set_report(all_case, report_path)
if __name__ == "__main__":
RunMain().run_case()
5、關(guān)鍵字驅(qū)動(dòng)模型
這是一種通過(guò)關(guān)鍵字的改變而引起測(cè)試結(jié)果改變的功能自動(dòng)化測(cè)試模型。QTP(UFT)、Robot Framework 等都是以關(guān)鍵字驅(qū)動(dòng)為主的自動(dòng)化測(cè)試工具,這類(lèi)工具典型的特征就是具備一套易用的可視化界面,測(cè)試人員需要做的就是將測(cè)試腳本按照“填表格”的方式填入,并考慮三個(gè)問(wèn)題就可以了:我要做什么?對(duì)誰(shuí)做?怎么做?
框架目錄:
action 主要存放動(dòng)作事件、元素操作。
Action.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
from AutomatedTestModel.KeywordDrivenModel.common.ExcelUtil import ExcelUtil
from AutomatedTestModel.KeywordDrivenModel.action.ElementOperation import ElementOperation
class Action:
def __init__(self):
self.element = ElementOperation()
def set_value(self, element, action, parameter=None):
if element == "browser":
return self.element.browser_operate(action, parameter)
elif element == "time":
return self.element.time_operate(action, parameter)
elif element is None or element == "":
return
else: # 如果不是其他的關(guān)鍵字,則默認(rèn)為定位的元素
return self.element.element_operate(element, action, parameter)
def case_operate(self, excel, sheet):
all_case = ExcelUtil(excel_path=excel, sheet_name=sheet).get_case()
for case in all_case:
self.set_value(case[0], case[1], case[2])
if __name__ == '__main__':
excel = '../case/casedata.xlsx'
Action().case_operate(excel=excel, sheet='搜索')
ElementOperation.py:
case 存放測(cè)試用例步驟。
common 存放公共的方法等。
如讀取 Excel 方法等。
RunMain.py 運(yùn)行腳本。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
from AutomatedTestModel.KeywordDrivenModel.action.Action import Action
if __name__ == '__main__':
excel = 'case/casedata.xlsx'
a = Action().case_operate(excel=excel, sheet='搜索')
6、行為驅(qū)動(dòng)模型
行為驅(qū)動(dòng)開(kāi)發(fā)(Behave Driven Development,簡(jiǎn)稱(chēng)BDD),即從用戶的需求出發(fā)強(qiáng)調(diào)系統(tǒng)行為。通過(guò)將BDD借鑒到自動(dòng)化測(cè)試中,便產(chǎn)生了行為驅(qū)動(dòng)測(cè)試模型,這種模型通過(guò)使用自然描述語(yǔ)言確定自動(dòng)化測(cè)試腳本,其優(yōu)點(diǎn)是可使用自然語(yǔ)言編寫(xiě)測(cè)試用例。
框架目錄:
features 存放用例。
steps 存放步驟:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號(hào):AllTests軟件測(cè)試
import time
from behave import *
@When('打開(kāi)訪問(wèn)的網(wǎng)頁(yè) "{url}"')
def step_open(context, url):
context.driver.get(url)
time.sleep(5)
@Then('進(jìn)入百度網(wǎng)站成功')
def step_assert_open(context):
title = context.driver.title
assert title == "百度一下,你就知道"
@When('輸入 "{searchdata}"')
def step_search(context, searchdata):
searchdata_element = context.driver.find_element_by_id('kw')
searchdata_element.send_keys(searchdata)
time.sleep(1)
submit_btn = context.driver.find_element_by_id('su')
submit_btn.click()
@Then('獲取標(biāo)題')
def step_assert_search(context):
success_message = context.driver.title
assert success_message == "自動(dòng)化測(cè)試_百度搜索"
environment.py 存放變量。
search.feature 存放行為。
report、result 存放報(bào)告。