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)文章
python 申請(qǐng)內(nèi)存空間,用于創(chuàng)建多維數(shù)組的實(shí)例
今天小編就為大家分享一篇python 申請(qǐng)內(nèi)存空間,用于創(chuàng)建多維數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
詳解Python中的分組函數(shù)groupby和itertools)
這篇文章主要介紹了Python中的分組函數(shù)groupby和itertools)的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
Python django框架 web端視頻加密的實(shí)例詳解
這篇文章主要介紹了Python django框架 web端視頻加密,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)
這篇文章主要介紹了python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python實(shí)現(xiàn)截圖生成符合markdown的鏈接
之前是用的是typora來寫的文章,最近typora最近開始收費(fèi)了,所以就不想用了,于是找到了一個(gè)替代品MarkText。本文將介紹如何通過Python實(shí)現(xiàn)截圖自動(dòng)生成符合markdown的鏈接,感興趣的可以了解一下2022-01-01
Python中字典和集合學(xué)習(xí)小結(jié)
本文通過實(shí)例給大家介紹了python中字典和集合的知識(shí)小結(jié),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07

