pytest系列(五) - fixture 進階玩法 (2)


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("-------------------------")

 

 

執行結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM