pytest 15 fixture之autouse=True
生活随笔
收集整理的這篇文章主要介紹了
pytest 15 fixture之autouse=True
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
平常寫自動化用例會寫一些前置的fixture操作,用例需要用到就直接傳該函數的參數名稱就行了。當用例很多的時候,每次都傳這個參數,會比較麻煩。
fixture里面有個參數autouse,默認是Fasle沒開啟的,可以設置為True開啟自動使用fixture功能,這樣用例就不用每次都去傳參了
調用fixture三種方法
-
1.函數或類里面方法直接傳fixture的函數參數名稱
-
2.使用裝飾器@pytest.mark.usefixtures()修飾
-
3.autouse=True自動使用
-
用例傳fixture參數
方法一:先定義start功能,用例全部傳start參數,調用該功能
#!/usr/bin/env/python # -*-coding:utf-8-*-import pytest@pytest.fixture(scope="function") def start(request):print('\n-----開始執行function----')def test_a(start):print("-------用例a執行-------")class Test_aaa():def test_01(self, start):print('-----------用例01--------------')def test_02(self, start):print('-----------用例02------------')運行結果:
============================= 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 -----開始執行function---- .-------用例a執行------------開始執行function---- .-----------用例01-------------------開始執行function---- .-----------用例02------------[100%]=========================== 3 passed in 0.02 seconds ===========================裝飾器usefixtures
方法二:使用裝飾器@pytest.mark.usefixtures()修飾需要運行的用例
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest@pytest.fixture(scope="function") def start(request):print('\n-----開始執行function----')@pytest.mark.usefixtures("start") def test_a():print("-------用例a執行-------")@pytest.mark.usefixtures("start") class Test_aaa():def test_01(self):print('-----------用例01--------------')def test_02(self):print('-----------用例02------------')運行結果:
============================= 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 -----開始執行function---- .-------用例a執行------------開始執行function---- .-----------用例01-------------------開始執行function---- .-----------用例02------------[100%]=========================== 3 passed in 0.02 seconds =========================== Process finished with exit code 0設置autouse=True
方法三、autouse設置為True,自動調用fixture功能
-
start設置scope為module級別,在當前.py用例模塊只執行一次,autouse=True自動使用
-
open_home設置scope為function級別,每個用例前都調用一次,自動使用
運行結果:
============================= 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 -----開始執行moule---- module : test_03 ----------啟動瀏覽器--------- function:test_01 --------回到首頁-------- .-----------用例01--------------function:test_02 --------回到首頁-------- .-----------用例02------------------------結束測試 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-----開始執行moule----')print('module : %s' % request.module.__name__)print('----------啟動瀏覽器---------')yieldprint("------------結束測試 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--------------\n')def test_02(self):print('-----------用例02------------\n')運行結果:
============================= 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 -----開始執行moule---- module : test_04 ----------啟動瀏覽器--------- function:test_01 --------回到首頁-------- .-----------用例01--------------function:test_02 --------回到首頁-------- .-----------用例02------------------------結束測試 end!-----------[100%]=========================== 2 passed in 0.02 seconds =========================== Process finished with exit code 0?
轉載于:https://www.cnblogs.com/peiminer/p/9946607.html
總結
以上是生活随笔為你收集整理的pytest 15 fixture之autouse=True的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python网络编程——实现简单聊天
- 下一篇: LeetCode 168. Excel