Pytest框架之fixture詳解(三)
相關(guān)文章
本文關(guān)于fixture的內(nèi)容如下:
- 1、參數(shù)化fixture
- 2、fixture工廠
- 3、request這個(gè)fixture
1、參數(shù)化fixture
fixture有個(gè)params參數(shù),允許我們傳遞數(shù)據(jù)。
語法格式:
# conftest.py文件 ? # fixture的params參數(shù) # 取value1時(shí),會(huì)把依賴此fixture的用例執(zhí)行一遍。 # 取value2時(shí),會(huì)把依賴此fixture的用例執(zhí)行一遍。 # 取value3時(shí),會(huì)把依賴此fixture的用例執(zhí)行一遍。 # params有幾個(gè)參數(shù),就會(huì)將依賴此fixture的用例執(zhí)行幾遍。 @pytest.fixture(params=[value1, value2, value3..]) def fix_name(): # do something
當(dāng)我們需要多次調(diào)用fixture時(shí),則可以用到fixture的參數(shù)化功能。
但它并不是并發(fā)的,是串行執(zhí)行的。
比如,測(cè)試對(duì)象有多種配置方式,那么參數(shù)化可以幫我們?cè)诙喾N配置方式下執(zhí)行用例。
接下來,以網(wǎng)頁自動(dòng)化為案例。
需求:要在google、firefox瀏覽器下執(zhí)行測(cè)試用例,用例為打開百度搜索pytest。
1)先在conftest.py當(dāng)中,定義fixture,并設(shè)置params=["google", "firefox"]
# conftest.py ? # params設(shè)置為google和firefox @pytest.fixture(params=["google", "firefox"]) def browser_fix(request): if request.param == "google": driver = webdriver.Chrome() elif request.param == "firefox": driver = webdriver.Firefox() else: driver = None yield driver if driver: driver.quit()
2)在測(cè)試用例文件test_baidu_action.py中,編寫測(cè)試用例,并調(diào)用browser_fix
# test_baidu_action.py ? @pytest.mark.usefixtures("browser_fix") def test_baidu(browser_fix): driver = browser_fix driver.get("https://www.baidu.com/") driver.find_element(By.ID, "kw").send_keys("pytest", Keys.ENTER) loc = (By.XPATH, '//h3') WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc)) driver.find_element(*loc).click()
3)運(yùn)行2)中的用例,會(huì)依次在google瀏覽器中執(zhí)行完成,然后在firefox瀏覽器中執(zhí)行完成。一共是2條測(cè)試用例。
2、fixture工廠
當(dāng)我們?cè)谝粋€(gè)用例當(dāng)中,需要多次調(diào)用fixture時(shí),就可以使用fixture工廠
利用的是裝飾器的方式
在fixture內(nèi)部,定義一個(gè)函數(shù)。fixture返回的是函數(shù)。
以下案例來自官網(wǎng):
@pytest.fixture def make_customer_record(): def _make_customer_record(name): return {"name": name, "orders": []} ? return _make_customer_record ? # 用例內(nèi)部,多次調(diào)用了fixture. def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") # 第1次調(diào)用 customer_2 = make_customer_record("Mike") # 第2次調(diào)用 customer_3 = make_customer_record("Meredith") # 第3次調(diào)用
如果工廠創(chuàng)建的數(shù)據(jù)需要管理,那么fixtue可以如下處理:
@pytest.fixture def make_customer_record(): # 管理工廠的數(shù)據(jù)。在前置中創(chuàng)建。在后置中銷毀 created_records = [] ? def _make_customer_record(name): record = models.Customer(name=name, orders=[]) # 前置中添加數(shù)據(jù) created_records.append(record) return record ? yield _make_customer_record # 返回內(nèi)部函數(shù) # 銷毀數(shù)據(jù) for record in created_records: record.destroy() ? # 測(cè)試用例 def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") customer_2 = make_customer_record("Mike") customer_3 = make_customer_record("Meredith")
3、request這個(gè)fixture
pytest內(nèi)置的名為requests的fixture,主要功能: 提供請(qǐng)求fixture的測(cè)試用例/測(cè)試類的信息的。
我們定義fixture之后,通常都是測(cè)試用例/測(cè)試類,來請(qǐng)求fixture。
而request fixture就會(huì)記錄 測(cè)試用例/測(cè)試類 相關(guān)信息。
request fixture是通過FixtureRequest來實(shí)現(xiàn)的,有以下屬性(列舉部分)可以使用:
- request.param:獲取fixture的params參數(shù)值
- request.scope:獲取fixture的作用域
- request.function:獲取調(diào)用fixture的用例函數(shù)名稱。如果fixture是函數(shù)級(jí)別的作用域。
- request.cls:獲取測(cè)試用例是從哪個(gè)測(cè)試類里收集的。
- request.module:獲取測(cè)試用例/測(cè)試類從哪個(gè)python模塊里收集的。
- request.config:從pytest的config文件當(dāng)中,獲取與當(dāng)前請(qǐng)求有關(guān)的配置信息
更多的請(qǐng)查閱官網(wǎng):https://docs.pytest.org/en/stable/reference.html
既然requests是fixture,那么我們定義的fixture,就可以直接把requests作為函數(shù)參數(shù)來用。
下面,以簡(jiǎn)單案例來演示。
定義一個(gè)fixture,將requests作為參數(shù)。
import pytest ? @pytest.fixture(params=[1,2]) def init(request): print("用例名稱:", request.function) print("fix參數(shù) ", request.param) print("fix的作用域 ", request.scope) print("用例所在的類 ", request.cls)
定義一個(gè)測(cè)試類,直接請(qǐng)求名為init的fixture:
@pytest.mark.usefixtures("init") class TestABC: ? def test_hello(self): print("-------------------------")
執(zhí)行結(jié)果如下:
到此這篇關(guān)于Pytest框架之fixture的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ubuntu下升級(jí) python3.7.1流程備忘(推薦)
這篇文章主要介紹了Ubuntu下升級(jí) python3.7.1流程備忘,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12翻轉(zhuǎn)數(shù)列python實(shí)現(xiàn),求前n項(xiàng)和,并能輸出整個(gè)數(shù)列的案例
這篇文章主要介紹了翻轉(zhuǎn)數(shù)列python實(shí)現(xiàn),求前n項(xiàng)和,并能輸出整個(gè)數(shù)列的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python和JS反爬之解決反爬參數(shù)?signKey
這篇文章主要介紹了Python和JS反爬之解決反爬參數(shù)?signKey,Python?反爬中有一大類,叫做字體反爬,核心的理論就是通過字體文件或者?CSS?偏移,接下來文章的詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05python實(shí)現(xiàn)數(shù)據(jù)庫(kù)跨服務(wù)器遷移
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)數(shù)據(jù)庫(kù)之間的數(shù)據(jù)遷移,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Python繪圖系統(tǒng)之繪制散點(diǎn)圖,極坐標(biāo)和子圖
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)一個(gè)繪圖系統(tǒng),可以支持繪制散點(diǎn)圖,極坐標(biāo)和子圖,文中的示例代碼講解詳細(xì),感興趣的可以了解下2023-09-09python腳本設(shè)置超時(shí)機(jī)制系統(tǒng)時(shí)間的方法
這篇文章主要介紹了python腳本設(shè)置超時(shí)機(jī)制系統(tǒng)時(shí)間的方法,感興趣的小伙伴們可以參考一下2016-02-02