1.前言
平常寫自動化用例會寫一些前置的fixture操作,用例需要用到就直接傳該函數(shù)的參數(shù)名稱就行了。當(dāng)用例很多的時候,每次都傳這個參數(shù),會比較麻煩。
fixture里面有個參數(shù)autouse,默認是Fasle沒開啟的,可以設(shè)置為True開啟自動使用fixture功能,這樣用例就不用每次都去傳參了
調(diào)用fixture三種方法
- 函數(shù)或類里面方法直接傳fixture的函數(shù)參數(shù)名稱
- 使用裝飾器@pytest.mark.usefixtures()修飾
- autouse=True自動使用
2.用例傳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-----開始執(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"])
3.裝飾器usefixtures
方法二:使用裝飾器@pytest.mark.usefixtures()修飾需要運行的用例
# content of test_07.py
import time
import pytest
@pytest.fixture(scope="function")
def start(request):
print('\n-----開始執(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"])
4.設(shè)置autouse=True
方法三、autouse設(shè)置為True,自動調(diào)用fixture功能
- start設(shè)置scope為module級別,在當(dāng)前.py用例模塊只執(zhí)行一次,autouse=True自動使用
- open_home設(shè)置scope為function級別,每個用例前都調(diào)用一次,自動使用
# content of test_08.py
import time
import pytest
@pytest.fixture(scope="module", autouse=True)
def start(request):
print('\n-----開始執(zhí)行moule----')
print('module : %s' % request.module.__name__)
print('----------啟動瀏覽器---------')
yield
print("------------結(jié)束測試 end!-----------")
@pytest.fixture(scope="function", autouse=True)
def open_home(request):
print("function:%s \n--------回到首頁--------" % request.function.__name__)
def test_01():
print('-----------用例01--------------')
def test_02():
print('-----------用例02------------')
if __name__ == "__main__":
pytest.main(["-s", "test_08.py"])
運行結(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
..\..\..\..\..\..\YOYO\peizhi\test_08.py
-----開始執(zhí)行moule----
module : YOYO.peizhi.test_08
----------啟動瀏覽器---------
function:test_01
--------回到首頁--------
-----------用例01--------------
.function:test_02
--------回到首頁--------
-----------用例02------------
.------------結(jié)束測試-----------
========================== 2 passed in 0.01 seconds ===========================
上面是函數(shù)去實現(xiàn)用例,寫的class里也是一樣可以的
# content of test_09.py
import time
import pytest
@pytest.fixture(scope="module", autouse=True)
def start(request):
print('\n-----開始執(zhí)行moule----')
print('module : %s' % request.module.__name__)
print('----------啟動瀏覽器---------')
yield
print("------------結(jié)束測試 end!-----------")
class Test_aaa():
@pytest.fixture(scope="function", autouse=True)
def open_home(self, request):
print("function:%s \n--------回到首頁--------" % request.function.__name__)
def test_01(self):
print('-----------用例01--------------')
def test_02(self):
print('-----------用例02------------')
if __name__ == "__main__":
pytest.main(["-s", "test_09.py"])