pytest的fixture有很多用法,本文在上兩篇的文章繼續補充fixture的使用,
第一篇文章地址:https://www.cnblogs.com/Simple-Small/p/13878172.html
第二篇文章地址:https://www.cnblogs.com/Simple-Small/p/14464878.html
本文關于fixture的內容如下:
1、引數化fixture
2、fixture工廠
3、request這個fixture
1、引數化fixture
fixture有個params引數,允許我們傳遞資料,
語法格式:
# conftest.py檔案 ? # fixture的params引數 # 取value1時,會把依賴此fixture的用例執行一遍, # 取value2時,會把依賴此fixture的用例執行一遍, # 取value3時,會把依賴此fixture的用例執行一遍, # params有幾個引數,就會將依賴此fixture的用例執行幾遍, @pytest.fixture(params=[value1, value2, value3..]) def fix_name(): # do something
當我們需要多次呼叫fixture時,則可以用到fixture的引數化功能,
但它并不是并發的,是串行執行的,
比如,測驗物件有多種配置方式,那么引數化可以幫我們在多種配置方式下執行用例,
接下來,以網頁自動化為案例,
需求:要在google、firefox瀏覽器下執行測驗用例,用例為打開百度搜索pytest,
1)先在conftest.py當中,定義fixture,并設定params=["google", "firefox"]
# conftest.py ? # params設定為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)在測驗用例檔案test_baidu_action.py中,撰寫測驗用例,并呼叫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)運行2)中的用例,會依次在google瀏覽器中執行完成,然后在firefox瀏覽器中執行完成,一共是2條測驗用例,

2、fixture工廠
當我們在一個用例當中,需要多次呼叫fixture時,就可以使用fixture工廠
利用的是裝飾器的方式
在fixture內部,定義一個函式,fixture回傳的是函式,
以下案例來自官網:
@pytest.fixture def make_customer_record(): def _make_customer_record(name): return {"name": name, "orders": []} ? return _make_customer_record ? # 用例內部,多次呼叫了fixture. def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") # 第1次呼叫 customer_2 = make_customer_record("Mike") # 第2次呼叫 customer_3 = make_customer_record("Meredith") # 第3次呼叫
如果工廠創建的資料需要管理,那么fixtue可以如下處理:
@pytest.fixture def make_customer_record(): # 管理工廠的資料,在前置中創建,在后置中銷毀 created_records = [] ? def _make_customer_record(name): record = models.Customer(name=name, orders=[]) # 前置中添加資料 created_records.append(record) return record ? yield _make_customer_record # 回傳內部函式 # 銷毀資料 for record in created_records: record.destroy() ? # 測驗用例 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這個fixture
pytest內置的名為requests的fixture,主要功能: 提供請求fixture的測驗用例/測驗類的資訊的,
我們定義fixture之后,通常都是測驗用例/測驗類,來請求fixture,
而request fixture就會記錄 測驗用例/測驗類 相關資訊,
request fixture是通過FixtureRequest來實作的,有以下屬性(列舉部分)可以使用:
request.param:獲取fixture的params引數值
request.scope:獲取fixture的作用域
request.function:獲取呼叫fixture的用例函式名稱,如果fixture是函式級別的作用域,
request.cls:獲取測驗用例是從哪個測驗類里收集的,
request.module:獲取測驗用例/測驗類從哪個python模塊里收集的,
request.config:從pytest的config檔案當中,獲取與當前請求有關的配置資訊
更多的請查閱官網:https://docs.pytest.org/en/stable/reference.html#request
既然requests是fixture,那么我們定義的fixture,就可以直接把requests作為函式引數來用,
下面,以簡單案例來演示,
定義一個fixture,將requests作為引數,
import pytest ? @pytest.fixture(params=[1,2]) def init(request): print("用例名稱:", request.function) print("fix引數 ", request.param) print("fix的作用域 ", request.scope) print("用例所在的類 ", request.cls)
定義一個測驗類,直接請求名為init的fixture:
@pytest.mark.usefixtures("init") class TestABC: ? def test_hello(self): print("-------------------------")
執行結果如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/285652.html
標籤:其他
上一篇:從工地實習月薪3K到一線企業年薪30W,我追上了那個曾經被賦予厚望的自己
下一篇:軟體測驗分類
