自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python自動化測試的五種模型

開發(fā) 測試
本篇將列舉實際自動化測試中,Python 自動化測試的五種模型:線性模型、模塊化驅(qū)動模型、數(shù)據(jù)驅(qū)動模型、關(guān)鍵字驅(qū)動模型、行為驅(qū)動模型。

1、前言

在自動化測試中,我們往往將自動化腳本都歸納屬于哪種框架模型,比如關(guān)鍵字驅(qū)動模型等。

本篇將列舉實際自動化測試中,Python 自動化測試的五種模型:線性模型、模塊化驅(qū)動模型、數(shù)據(jù)驅(qū)動模型、關(guān)鍵字驅(qū)動模型、行為驅(qū)動模型。

2、線性模型

通過錄制或編寫腳本,一個腳本完成一個場景(一組完整功能操作),通過對腳本的回放進行自動化測試。

腳本代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

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('自動化測試')
time.sleep(1)
driver.find_element_by_id('su').click()
time.sleep(1)
driver.quit()

3、模塊化驅(qū)動模型

將腳本中重復(fù)可復(fù)用的部分拿出來寫成一個公共的模塊,需要的時候就調(diào)用它,這樣可以大幅提高測試人員編寫腳本的效率。

框架目錄:

config 存放配置文件。

例如 base_data.json 文件,存放測試地址。

{
  "url": "https://www.baidu.com/"
}

data 存放測試數(shù)據(jù)。

drivers 存放瀏覽器驅(qū)動文件。

report 存放執(zhí)行完成后的測試報告。

test 存放測試用例。

case 測試用例步驟。

例如 testSearch.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

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('自動化測試')

if __name__ == '__main__':
    unittest.main()

common 存放公共的方法等。

pages 存放頁面元素與頁面操作。

例如 searchPage.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

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 存放運行腳本。

例如 main.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

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

        # 獲取當前時間
        now = time.strftime('%Y{y}%m{m}%dk6zqhab033oa%H{h}%M{M}%S{s}').format(y="年", m="月", d="日", h="時", M="分", s="秒")
        # 標題
        title = u"搜索測試"
        # 設(shè)置報告存放路徑和命名
        report_abspath = os.path.join(report_path, title + now + ".html")
        # 測試報告寫入
        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)出報告樣式、讀取配置文件等。

run.py 運行腳本。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

from AutomatedTestModel.ModularDriverModel.test.runner.main import Main

if __name__ == '__main__':
    Main().run_case()

運行后的測試報告。

4、數(shù)據(jù)驅(qū)動模型

該模型會根據(jù)數(shù)據(jù)的變化而引起測試結(jié)果的改變,這顯然是一個非常高級的概念和想法。簡單地說,該模型是一種數(shù)據(jù)的參數(shù)化呈現(xiàn),即通過輸入不同的參數(shù)來驅(qū)動程序執(zhí)行,輸出不同的測試結(jié)果。

框架目錄:

case 存放測試用例步驟。

common 存放公共的方法等。

如讀取 Excel 方法、生成報告等樣式。

data 存放測試數(shù)據(jù)與預(yù)期結(jié)果。

report 存放執(zhí)行完成后的測試報告。

打開報告效果。

RunMain.py 運行腳本。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

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

        # 獲取當前時間
        now = time.strftime('%Y{y}%m{m}%dk6zqhab033oa%H{h}%M{M}%S{s}').format(y="年", m="月", d="日", h="時", M="分", s="秒")
        # 標題
        title = u"搜索測試"
        # 設(shè)置報告存放路徑和命名
        report_abspath = os.path.join(report_path, title + now + ".html")
        # 測試報告寫入
        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ū)動模型

這是一種通過關(guān)鍵字的改變而引起測試結(jié)果改變的功能自動化測試模型。QTP(UFT)、Robot Framework 等都是以關(guān)鍵字驅(qū)動為主的自動化測試工具,這類工具典型的特征就是具備一套易用的可視化界面,測試人員需要做的就是將測試腳本按照“填表格”的方式填入,并考慮三個問題就可以了:我要做什么?對誰做?怎么做?

框架目錄:

action 主要存放動作事件、元素操作。

Action.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

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)鍵字,則默認為定位的元素
            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 存放測試用例步驟。

common 存放公共的方法等。

如讀取 Excel 方法等。

RunMain.py 運行腳本。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

from AutomatedTestModel.KeywordDrivenModel.action.Action import Action

if __name__ == '__main__':
    excel = 'case/casedata.xlsx'
    a = Action().case_operate(excel=excel, sheet='搜索')

6、行為驅(qū)動模型

行為驅(qū)動開發(fā)(Behave Driven Development,簡稱BDD),即從用戶的需求出發(fā)強調(diào)系統(tǒng)行為。通過將BDD借鑒到自動化測試中,便產(chǎn)生了行為驅(qū)動測試模型,這種模型通過使用自然描述語言確定自動化測試腳本,其優(yōu)點是可使用自然語言編寫測試用例。

框架目錄:

features 存放用例。

steps 存放步驟:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公眾號:AllTests軟件測試

import time
from behave import *

@When('打開訪問的網(wǎng)頁 "{url}"')
def step_open(context, url):
    context.driver.get(url)
    time.sleep(5)

@Then('進入百度網(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('獲取標題')
def step_assert_search(context):
    success_message = context.driver.title
    assert success_message == "自動化測試_百度搜索"

environment.py 存放變量。

search.feature 存放行為。

report、result 存放報告。

責任編輯:姜華 來源: AllTests軟件測試
相關(guān)推薦

2019-04-22 09:00:00

Python框架自動化測試

2019-04-18 09:00:00

Java自動化測試框架

2021-03-23 08:00:00

工具開發(fā)審查

2012-02-27 17:34:12

Facebook自動化

2022-02-17 10:37:16

自動化開發(fā)團隊預(yù)測

2023-10-30 17:41:29

機器人自動化

2022-10-17 15:59:40

Shell腳本終端

2022-02-17 13:03:28

Python腳本代碼

2022-06-08 14:22:55

自動化測試測試

2022-05-10 11:18:42

自動化測試軟件測試

2024-11-21 15:24:49

2023-03-20 15:14:39

視覺回歸測試軟件開發(fā)

2023-03-27 15:37:43

自動化測試開發(fā)

2014-04-16 14:15:01

QCon2014

2013-05-16 10:58:44

Android開發(fā)自動化測試

2022-03-15 09:11:42

Python編程模式數(shù)據(jù)類型

2021-09-03 09:56:18

鴻蒙HarmonyOS應(yīng)用

2024-01-19 16:56:04

軟件測試

2021-06-30 19:48:21

前端自動化測試Vue 應(yīng)用

2012-12-24 22:54:31

點贊
收藏

51CTO技術(shù)棧公眾號