亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python中Pytest測試框架的fixture使用詳解

 更新時間:2023年08月03日 11:00:56   作者:橙子軟件測試菇?jīng)? 
這篇文章主要介紹了Python中Pytest測試框架的fixture使用詳解,Pytest的fixture的目的是提供一個測試的基線,在此基線基礎(chǔ)上,可以更可靠的進(jìn)行重復(fù)測試,需要的朋友可以參考下

1、fixture的含義

fixture的目的是提供一個測試的基線,在此基線基礎(chǔ)上,可以更可靠的進(jìn)行重復(fù)測試。

2、fixture的優(yōu)勢

Pytest的fixture相對于傳統(tǒng)的xUnit的setup/teardown函數(shù)做了顯著的改進(jìn):

(1)測試fixture有明確的名稱,通過在函數(shù)/模塊/類或者整個項(xiàng)目中激活來使用

(2)測試fixture是模塊化的實(shí)現(xiàn),使用fixture名即可觸發(fā)特定的fixture,fixture可以在其他fixture中進(jìn)行使用測試fixture不僅可以進(jìn)行簡單的單元測試,也可以進(jìn)行復(fù)雜的功能測試??梢愿鶕?jù)配置和組件的選項(xiàng)進(jìn)行參數(shù)化定制測試,或者跨函數(shù)/類/模塊或者整個測試過程進(jìn)行測試。

(3)pytest依然支持經(jīng)典的xUnit的樣式,你可以根據(jù)自己的喜好混合兩種樣式。

3、fixture參數(shù)

  • scope
  • params
  • autouse
  • ids
  • name
def fixture(
    fixture_function: Optional[_FixtureFunction] = None,
    *,
    scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
    params: Optional[Iterable[object]] = None,
    autouse: bool = False,
    ids: Optional[
        Union[
            Iterable[Union[None, str, float, int, bool]],
            Callable[[Any], Optional[object]],
        ]
    ] = None,
    name: Optional[str] = None,
) -> Union[FixtureFunctionMarker, _FixtureFunction]:
    """Decorator to mark a fixture factory function.
    This decorator can be used, with or without parameters, to define a
    fixture function.
    The name of the fixture function can later be referenced to cause its
    invocation ahead of running tests: test modules or classes can use the
    ``pytest.mark.usefixtures(fixturename)`` marker.
    Test functions can directly use fixture names as input arguments in which
    case the fixture instance returned from the fixture function will be
    injected.
    Fixtures can provide their values to test functions using ``return`` or
    ``yield`` statements. When using ``yield`` the code block after the
    ``yield`` statement is executed as teardown code regardless of the test
    outcome, and must yield exactly once.
    :param scope:
        The scope for which this fixture is shared; one of ``"function"``
        (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
        This parameter may also be a callable which receives ``(fixture_name, config)``
        as parameters, and must return a ``str`` with one of the values mentioned above.
        See :ref:`dynamic scope` in the docs for more information.
    :param params:
        An optional list of parameters which will cause multiple invocations
        of the fixture function and all of the tests using it. The current
        parameter is available in ``request.param``.
    :param autouse:
        If True, the fixture func is activated for all tests that can see it.
        If False (the default), an explicit reference is needed to activate
        the fixture.
    :param ids:
        List of string ids each corresponding to the params so that they are
        part of the test id. If no ids are provided they will be generated
        automatically from the params.
    :param name:
        The name of the fixture. This defaults to the name of the decorated
        function. If a fixture is used in the same module in which it is
        defined, the function name of the fixture will be shadowed by the
        function arg that requests the fixture; one way to resolve this is to
        name the decorated function ``fixture_<fixturename>`` and then use
        ``@pytest.fixture(name='<fixturename>')``.
    """
    fixture_marker = FixtureFunctionMarker(
        scope=scope, params=params, autouse=autouse, ids=ids, name=name,
    )
    # Direct decoration.
    if fixture_function:
        return fixture_marker(fixture_function)
    return fixture_marker

4、實(shí)戰(zhàn)一:fixture作為參數(shù)使用

測試函數(shù)可以通過接受一個已經(jīng)命名的fixture對象來使用它們。對于每個參數(shù)名,如果fixture已經(jīng)聲明定義,會自動創(chuàng)建一個實(shí)例并傳入該測試函數(shù)。

fixture函數(shù)通過裝飾器標(biāo)志@pytest.fixture來注冊。

下面是一個簡單的獨(dú)立的測試模塊,包含一個fixture及使用它的測試函數(shù)。

使用場景:

  • 用例1需要登錄
  • 用例2不需要登錄
  • 用例3需要登錄

用法

在方法前面加@pytest.fixtrue()

實(shí)戰(zhàn)

  • 單個fixture
import pytest
@pytest.fixture()
def login():
    print("這個是登錄方法")
def test_case1(login):
    print("test_case1,需要登錄")
    pass
def test_case2():
    print("test_case2,不用登錄")
    pass
def test_case3(login):
    print("test_case3,需要登錄")
    pass
if __name__ == '__main__':
    pytest.main()

在這里插入圖片描述

  • 多個fixture
import pytest
//名字可以作為參數(shù)
@pytest.fixture()
def login():
    print("這個是登錄方法")
    return ('tom',"123")
@pytest.fixture()
def operate():
    print("登錄后的操作")
def test_case1(login,operate):
    print(login)
def test_case2():
    print("test_case2,不用登錄")
def test_case3(login):
    print(login)
if __name__ == '__main__':
    pytest.main()

在這里插入圖片描述

5、實(shí)戰(zhàn)二:fixture的作用范圍

測試函數(shù)可以通過接受一個已經(jīng)命名的fixture對象來使用他們。

對于每個參數(shù)名,如果fixture已聲明定義,會自動創(chuàng)建一個實(shí)例并傳入該測試函數(shù)。fixture函數(shù)通過裝飾器標(biāo)志@pytest.fixture來注冊。

下面是一個簡單的獨(dú)立的測試模塊,包含一個fixture及使用它的測試函數(shù)

名稱范圍說明
function函數(shù)級每一個函數(shù)或方法都會調(diào)用
class類級別每個測試類只運(yùn)行一次
module模塊級每一個.py文件調(diào)用一次
package包級每一個python包只調(diào)用一次(暫不支持)
session會話級每次會話只需要運(yùn)行一次,會話內(nèi)所有方法及類,模塊都共享這個方法

作用范圍順序:session》module》class》function

使用場景

整個模塊有多條測試用例,需要在全部用例執(zhí)行之前打開瀏覽器,全部執(zhí)行之后去關(guān)閉瀏覽器,打開和關(guān)閉操作只執(zhí)行一次。

import pytest
# 作用域:module是在模塊之前執(zhí)行,模塊之后執(zhí)行
@pytest.fixture(scope="module")
def open():
    print("打開瀏覽器")
    yield
    print("執(zhí)行teardown!")
    print("最后關(guān)閉瀏覽器")
@pytest.mark.usefixtures("open")
def test_search1():
    print("test_search1")
def test_search2(open):
    print("test_search2")
def test_search3(open):
    print("test_search3")

return 與 yield的區(qū)別:

  • return:在程序函數(shù)中返回某個值,返回之后函數(shù)不在繼續(xù)執(zhí)行,徹底結(jié)束。
  • yield: 帶有yield的函數(shù)是一個迭代器,函數(shù)返回某個值時,會停留在某個位置,返回函數(shù)值后,會在前面停留的位置繼續(xù)執(zhí)行直到程序結(jié)束

6、實(shí)戰(zhàn)三:fixture與conftest.py結(jié)合使用

在使用之前我們先來了解什么是conftest.py?

實(shí)現(xiàn)測試用例的過程中,當(dāng)你發(fā)現(xiàn)需要使用來自多個文件的fixture函數(shù)的時候,可以將這些fixture函數(shù)放到conftest.py中。

你不需要導(dǎo)入這些fixture函數(shù),它會由pytest自動檢索。

fixture函數(shù)的檢索順序是從測試類開始,然后測試的模塊,然后就是conftest.py文件,最后是內(nèi)置的插件和第三方插件。

使用場景

你與其他測試工程師合作一起開發(fā)時,公共的模塊要在不同文件中,要在大家都訪問到的地方(建議可以放在項(xiàng)目的根目錄,因?yàn)檎业絚onftest.py文件,先在同級目錄找,如果同級目錄找不到時,會向上級目錄繼續(xù)找,指導(dǎo)找到為止)

ps:conftest.py名稱是固定,不能修改

實(shí)戰(zhàn)

conftest.py的內(nèi)容如下:

import pytest
#這個一定不要忘記寫
@pytest.fixture(scope="session")
def login():
    # setup操作
    print("用戶需要先完成登錄操作")
    username = "hxc"
    name = "pytest書籍"
    # 相當(dāng)于return,但是return不會執(zhí)行后面的語句,yield是可以
    yield username,name
    # teardown操作
    print("完成登出操作")

test_conftestDemo.py的內(nèi)容如下:

import pytest
def test_search(login):
    username,name = login
    print(f"用戶名:{username},搜索商品:{name}")
def test_cart(login):
    print("添加購物車")
def test_order():
    print("下單pytest書籍")
def test_login(login):
    print("添加購物車之前先登錄")
def test_get_product(connectDB):
    print("獲取商品信息先連接數(shù)據(jù)庫")
@pytest.fixture()
def connectDB():
    print("連接數(shù)據(jù)庫")
    yield
    print("斷開數(shù)據(jù)庫")

在這里插入圖片描述

7、實(shí)戰(zhàn)四:fixture參數(shù)化

測試過程中需要大量的測試數(shù)據(jù),如果每一個測試用例都編寫一條測試用例,用例將是非常龐大的。

一般會使用將測試用例用到的數(shù)據(jù)一參數(shù)的形式傳入待測試用例中,并為每條測試數(shù)據(jù)生成一個測試結(jié)果數(shù)據(jù)。

使用場景

測試離不開數(shù)據(jù),為了數(shù)據(jù)靈活,一般數(shù)據(jù)都是通過參數(shù)傳的

使用方法

在fixture方法上加裝飾器@pytest.fixture(params=[1,2,3]),就會傳入三個數(shù)據(jù)1,2,3,分別將這三個數(shù)據(jù)傳入到用例當(dāng)中。傳入的數(shù)據(jù)需要使用一個固定的參數(shù)名request來接收。

import pytest
@pytest.fixture(params=["selenium", "appium"])
def login(request):
    print(f"用戶名:{request.param}")
    return request.param
def test_demo1(login):
    print(f"demo1 case 數(shù)據(jù)為: {login}")

在這里插入圖片描述

import pytest
@pytest.fixture(params=[["selenium",123],["appium",123456]])
def login(request):
    print(f"用戶名:{request.param}")
    return request.param
def test_demo1(login):
    print(f"demo1 case: 數(shù)據(jù)為: {login}")

在這里插入圖片描述

到此這篇關(guān)于Python中Pytest測試框架的fixture使用詳解的文章就介紹到這了,更多相關(guān)Pytest框架的fixture內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論