一、前言
平常寫(xiě)自動(dòng)化用例會(huì)寫(xiě)一些前置的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)用該功能
# content of test_06.py
import time
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------------')
if __name__ == "__main__":
pytest.main(["-s", "test_06.py"])
三、裝飾器usefixtures
使用裝飾器@pytest.mark.usefixtures()修飾需要運(yùn)行的用例
# content of test_07.py
import time
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------------')
if __name__ == "__main__":
pytest.main(["-s", "test_07.py"])
四、設(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)使用
# content of test_08.py
import time
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)瀏覽器---------')
yield
print("------------結(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--------------')
def test_02():
print('-----------用例02------------')
if __name__ == "__main__":
pytest.main(["-s", "test_08.py"])
運(yùn)行結(jié)果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\, inifile:
plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items
..\..\..\..\..\..\MOMO\peizhi\test_08.py
-----開(kāi)始執(zhí)行moule----
module : MOMO.peizhi.test_08
----------啟動(dòng)瀏覽器---------
function:test_01
--------回到首頁(yè)--------
-----------用例01--------------
.function:test_02
--------回到首頁(yè)--------
-----------用例02------------
.------------結(jié)束測(cè)試-----------
========================== 2 passed in 0.01 seconds ===========================
上面是函數(shù)去實(shí)現(xiàn)用例,寫(xiě)的class里也是一樣可以的
# content of test_09.py
import time
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)瀏覽器---------')
yield
print("------------結(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--------------')
def test_02(self):
print('-----------用例02------------')
if __name__ == "__main__":
pytest.main(["-s", "test_09.py"])
最后感謝每一個(gè)認(rèn)真閱讀我文章的人,禮尚往來(lái)總是要有的,雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走:
【軟件測(cè)試技術(shù)交流(資料分享)】:320231853(備注C)http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=rS49sB1dBN6wjk4SbxAjX80YS65Zy8TH&authKey=tlP2KE7Sut5Dq7EvwkG55B%2B0sWc5WpLYbuRGFftTLHed0FB22lskhUs4Dnw6hQRP&noverify=0&group_code=320231853
生命不息,奮斗不止。每一份努力都不會(huì)被辜負(fù),只要堅(jiān)持不懈,終究會(huì)有回報(bào)。珍惜時(shí)間,追求夢(mèng)想。不忘初心,砥礪前行。你的未來(lái),由你掌握!
生命短暫,時(shí)間寶貴,我們無(wú)法預(yù)知未來(lái)會(huì)發(fā)生什么,但我們可以掌握當(dāng)下。珍惜每一天,努力奮斗,讓自己變得更加強(qiáng)大和優(yōu)秀。堅(jiān)定信念,執(zhí)著追求,成功終將屬于你!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-510363.html
只有不斷地挑戰(zhàn)自己,才能不斷地超越自己。堅(jiān)持追求夢(mèng)想,勇敢前行,你就會(huì)發(fā)現(xiàn)奮斗的過(guò)程是如此美好而值得。相信自己,你一定可以做到!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-510363.html
到了這里,關(guān)于從0到1精通自動(dòng)化測(cè)試,pytest自動(dòng)化測(cè)試框架,fixture之a(chǎn)utouse=True(十二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!