最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytest框架之fixture詳解(三)

 更新時(shí)間:2022年06月30日 10:19:35   作者:小旭2021  
本文詳細(xì)講解了Pytest框架之fixture,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

相關(guān)文章

Pytest框架之fixture詳解(一)

Pytest框架之fixture詳解(二)

Pytest框架之fixture詳解(三)

本文關(guān)于fixture的內(nèi)容如下:

  • 1、參數(shù)化fixture
  • 2、fixture工廠
  • 3、request這個fixture

1、參數(shù)化fixture

fixture有個params參數(shù),允許我們傳遞數(shù)據(jù)。

語法格式:

# conftest.py文件
?
# fixture的params參數(shù)
# 取value1時(shí),會把依賴此fixture的用例執(zhí)行一遍。
# 取value2時(shí),會把依賴此fixture的用例執(zhí)行一遍。
# 取value3時(shí),會把依賴此fixture的用例執(zhí)行一遍。
# params有幾個參數(shù),就會將依賴此fixture的用例執(zhí)行幾遍。
@pytest.fixture(params=[value1, value2, value3..])
def fix_name():
    # do something

當(dāng)我們需要多次調(diào)用fixture時(shí),則可以用到fixture的參數(shù)化功能。

但它并不是并發(fā)的,是串行執(zhí)行的。

比如,測試對象有多種配置方式,那么參數(shù)化可以幫我們在多種配置方式下執(zhí)行用例。

接下來,以網(wǎng)頁自動化為案例。

需求:要在google、firefox瀏覽器下執(zhí)行測試用例,用例為打開百度搜索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)在測試用例文件test_baidu_action.py中,編寫測試用例,并調(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)中的用例,會依次在google瀏覽器中執(zhí)行完成,然后在firefox瀏覽器中執(zhí)行完成。一共是2條測試用例。

2、fixture工廠

當(dāng)我們在一個用例當(dāng)中,需要多次調(diào)用fixture時(shí),就可以使用fixture工廠

利用的是裝飾器的方式

在fixture內(nèi)部,定義一個函數(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()
?
# 測試用例
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內(nèi)置的名為requests的fixture,主要功能: 提供請求fixture的測試用例/測試類的信息的。

我們定義fixture之后,通常都是測試用例/測試類,來請求fixture。

而request fixture就會記錄 測試用例/測試類 相關(guān)信息。

request fixture是通過FixtureRequest來實(shí)現(xiàn)的,有以下屬性(列舉部分)可以使用:

  • request.param:獲取fixture的params參數(shù)值
  • request.scope:獲取fixture的作用域
  • request.function:獲取調(diào)用fixture的用例函數(shù)名稱。如果fixture是函數(shù)級別的作用域。
  • request.cls:獲取測試用例是從哪個測試類里收集的。
  • request.module:獲取測試用例/測試類從哪個python模塊里收集的。
  • request.config:從pytest的config文件當(dāng)中,獲取與當(dāng)前請求有關(guān)的配置信息

更多的請查閱官網(wǎng):https://docs.pytest.org/en/stable/reference.html

既然requests是fixture,那么我們定義的fixture,就可以直接把requests作為函數(shù)參數(shù)來用。

下面,以簡單案例來演示。

定義一個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)

定義一個測試類,直接請求名為init的fixture:

@pytest.mark.usefixtures("init")
class TestABC:
?
    def test_hello(self):
        print("-------------------------")

執(zhí)行結(jié)果如下:

到此這篇關(guān)于Pytest框架之fixture的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

衡阳市| 九江县| 唐河县| 合江县| 香格里拉县| 洪江市| 浦城县| 富宁县| 荥经县| 阿尔山市| 丹巴县| 南宫市| 中江县| 报价| 彰化县| 丰台区| 陵水| 洱源县| 岫岩| 砚山县| 遂昌县| 澎湖县| 宜州市| 探索| 北安市| 龙门县| 无锡市| 安陆市| 同心县| 盐山县| 杨浦区| 芜湖县| 闸北区| 方山县| 增城市| 江口县| 山东省| 江西省| 临城县| 延川县| 雅江县|