日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

pytest 15 fixture之autouse=True

發(fā)布時(shí)間:2024/4/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytest 15 fixture之autouse=True 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

平常寫自動(dòng)化用例會(huì)寫一些前置的fixture操作,用例需要用到就直接傳該函數(shù)的參數(shù)名稱就行了。當(dāng)用例很多的時(shí)候,每次都傳這個(gè)參數(shù),會(huì)比較麻煩。
fixture里面有個(gè)參數(shù)autouse,默認(rèn)是Fasle沒(méi)開(kāi)啟的,可以設(shè)置為True開(kāi)啟自動(dòng)使用fixture功能,這樣用例就不用每次都去傳參了

調(diào)用fixture三種方法

    • 1.函數(shù)或類里面方法直接傳fixture的函數(shù)參數(shù)名稱

    • 2.使用裝飾器@pytest.mark.usefixtures()修飾

    • 3.autouse=True自動(dòng)使用

用例傳fixture參數(shù)

方法一:先定義start功能,用例全部傳start參數(shù),調(diào)用該功能

#!/usr/bin/env/python # -*-coding:utf-8-*-import pytest@pytest.fixture(scope="function") def start(request):print('\n-----開(kāi)始執(zhí)行function----')def test_a(start):print("-------用例a執(zhí)行-------")class Test_aaa():def test_01(self, start):print('-----------用例01--------------')def test_02(self, start):print('-----------用例02------------')

運(yùn)行結(jié)果:

============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 3 itemspython_work_apple/pytest_package/fixtureDemo/test_01.py -----開(kāi)始執(zhí)行function---- .-------用例a執(zhí)行------------開(kāi)始執(zhí)行function---- .-----------用例01-------------------開(kāi)始執(zhí)行function---- .-----------用例02------------[100%]=========================== 3 passed in 0.02 seconds ===========================

裝飾器usefixtures

方法二:使用裝飾器@pytest.mark.usefixtures()修飾需要運(yùn)行的用例

#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest@pytest.fixture(scope="function") def start(request):print('\n-----開(kāi)始執(zhí)行function----')@pytest.mark.usefixtures("start") def test_a():print("-------用例a執(zhí)行-------")@pytest.mark.usefixtures("start") class Test_aaa():def test_01(self):print('-----------用例01--------------')def test_02(self):print('-----------用例02------------')

運(yùn)行結(jié)果:

============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 3 itemspython_work_apple/pytest_package/fixtureDemo/test_02.py -----開(kāi)始執(zhí)行function---- .-------用例a執(zhí)行------------開(kāi)始執(zhí)行function---- .-----------用例01-------------------開(kāi)始執(zhí)行function---- .-----------用例02------------[100%]=========================== 3 passed in 0.02 seconds =========================== Process finished with exit code 0

設(shè)置autouse=True

方法三、autouse設(shè)置為True,自動(dòng)調(diào)用fixture功能

  • start設(shè)置scope為module級(jí)別,在當(dāng)前.py用例模塊只執(zhí)行一次,autouse=True自動(dòng)使用

  • open_home設(shè)置scope為function級(jí)別,每個(gè)用例前都調(diào)用一次,自動(dòng)使用

#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest@pytest.fixture(scope="module", autouse=True) def start(request):print('\n-----開(kāi)始執(zhí)行moule----')print('module : %s' % request.module.__name__)print('----------啟動(dòng)瀏覽器---------')yieldprint("------------結(jié)束測(cè)試 end!-----------")@pytest.fixture(scope="function", autouse=True) def open_home(request):print("function:%s \n--------回到首頁(yè)--------" % request.function.__name__)def test_01():print('-----------用例01--------------\n')def test_02():print('-----------用例02------------\n')

運(yùn)行結(jié)果:

============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 2 itemspython_work_apple/pytest_package/fixtureDemo/test_03.py -----開(kāi)始執(zhí)行moule---- module : test_03 ----------啟動(dòng)瀏覽器--------- function:test_01 --------回到首頁(yè)-------- .-----------用例01--------------function:test_02 --------回到首頁(yè)-------- .-----------用例02------------------------結(jié)束測(cè)試 end!-----------[100%]=========================== 2 passed in 0.02 seconds ===========================

?寫在class里面也是一樣的

#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest@pytest.fixture(scope="module", autouse=True) def start(request):print('\n-----開(kāi)始執(zhí)行moule----')print('module : %s' % request.module.__name__)print('----------啟動(dòng)瀏覽器---------')yieldprint("------------結(jié)束測(cè)試 end!-----------")class Test_aaa():@pytest.fixture(scope="function", autouse=True)def open_home(self, request):print("function:%s \n--------回到首頁(yè)--------" % request.function.__name__)def test_01(self):print('-----------用例01--------------\n')def test_02(self):print('-----------用例02------------\n')

運(yùn)行結(jié)果:

============================= test session starts ============================== platform darwin -- Python 3.7.0, pytest-3.9.1, py-1.7.0, pluggy-0.8.0 rootdir: /Users/newcomer/gitByMyself, inifile: plugins: datadir-1.2.1, allure-adaptor-1.7.10collected 2 itemspython_work_apple/pytest_package/fixtureDemo/test_04.py -----開(kāi)始執(zhí)行moule---- module : test_04 ----------啟動(dòng)瀏覽器--------- function:test_01 --------回到首頁(yè)-------- .-----------用例01--------------function:test_02 --------回到首頁(yè)-------- .-----------用例02------------------------結(jié)束測(cè)試 end!-----------[100%]=========================== 2 passed in 0.02 seconds =========================== Process finished with exit code 0

?

轉(zhuǎn)載于:https://www.cnblogs.com/peiminer/p/9946607.html

總結(jié)

以上是生活随笔為你收集整理的pytest 15 fixture之autouse=True的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。