016、fixture之 autouse 引數
阿新 • • 發佈:2021-08-07
1、fixture之 autouse 引數
概述:
a、autouse 引數,預設為False,需要使用時手動呼叫 , 如果設定為True,相關層級所有的用例都會自動使用這個fixture ;
b、fixture 的 autouse=True測試函式自動呼叫,不需要傳函式名 ,這樣也就無法獲取到 fixture 的返回值,一般不怎麼用 ;
a、autouse 引數,預設為False,需要使用時手動呼叫 , 如果設定為True,相關層級所有的用例都會自動使用這個fixture ;
示例結構層級如下:
conftest.py 程式碼如下:
import pytestView Code# 設定fixture,scope='class' ,autouse 的預設引數是:False, # 某個類需要呼叫時使用 @pytest.mark.usefixtures('open_browser') @pytest.fixture(scope='class') def open_browser(): print('開啟瀏覽器') # 設定fixture,scope='function' ,autouse 的預設引數是:False # 某個函式需要呼叫時,把 open_web 函式名傳給某個函式即可,比如:def test_aa_func_1(open_web) @pytest.fixture(scope='function') def open_web(): print('開啟網站') # 設定fixture,scope='function' ,autouse 設定為 True,所有的函式、方法都會預設呼叫login @pytest.fixture(scope='function', autouse=True) def login(): print('開始登入')
test_aa.py 程式碼如下:
import pytest # 當 autouse沒有為True時,需要手動呼叫; # 手動呼叫 open_web,fixture , def test_aa_func_1(open_web): aView Code= 'hello' b = 'world' print('\n============= test_aa_func_1 ==============') assert a in 'hello world' def test_aa_func_2(): a = 'hello' b = 'world' print('\n============= test_aa_func_2 ==============') assert a in 'hello world' # 手動呼叫 類 fixture; @pytest.mark.usefixtures('open_browser') class TestAA(): def test_aa_1(self): a = 'hello' b = 'world' print('\n============= test_aa_1 ==============') assert a in 'hello world' def test_aa_2(self): a = 'hello' b = 'world' print('\n============= test_aa_2 ==============') assert a in 'hello world' class TestAATemp(): def test_aa_temp_1(self, open_web): a = 'hello' b = 'world' print('\n============= test_aa_temp_1 ==============') assert a in 'hello world' def test_aa_temp_2(self): a = 'hello' b = 'world' print('\n============= test_aa_temp_2 ==============') assert a in 'hello world'
執行結果如下:
(venv) D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\aa>pytest -sv ======================================================================== test session starts ======================================================================== platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe cachedir: .pytest_cache rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\aa collected 6 items test_aa.py::test_aa_func_1 開始登入 開啟網站 ============= test_aa_func_1 ============== PASSED test_aa.py::test_aa_func_2 開始登入 ============= test_aa_func_2 ============== PASSED test_aa.py::TestAA::test_aa_1 開啟瀏覽器 開始登入 ============= test_aa_1 ============== PASSED test_aa.py::TestAA::test_aa_2 開始登入 ============= test_aa_2 ============== PASSED test_aa.py::TestAATemp::test_aa_temp_1 開始登入 開啟網站 ============= test_aa_temp_1 ============== PASSED test_aa.py::TestAATemp::test_aa_temp_2 開始登入 ============= test_aa_temp_2 ============== PASSED ========================================================================= 6 passed in 0.05s ========================================================================= (venv) D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\aa>View Code
b、fixture 的 autouse=True 測試函式自動呼叫,不需要傳函式名 ,這樣也就無法獲取到 fixture 的返回值 ,一般不怎麼用 ;
autouse=True示例程式碼如下:
import pytest @pytest.fixture(scope='function', autouse=True) def login(): print('\n登入成功') return 'login success' # login ,fixture 中的 autouse 已經設定為 True, # 測試函式自動呼叫,不需要傳函式名 ,這樣也就無法獲取到 login 的返回值了; def test_login_1(): print('\n========== test_login_1 =========') def test_login_2(): print('\n========== test_login_2 =========')View Code
執行結果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py Testing started at 10:06 ... Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd ============================= test session starts ============================= platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe cachedir: .pytest_cache rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd collecting ... collected 2 items test_dd.py::test_login_1 登入成功 PASSED [ 50%] ========== test_login_1 ========= test_dd.py::test_login_2 登入成功 PASSED [100%] ========== test_login_2 ========= ============================== 2 passed in 0.01s ============================== Process finished with exit code 0View Code
autouse=Fasle ,有返回值,示例程式碼如下:
import pytest # autouse預設為False @pytest.fixture(scope='function') def login(): print('\n登入成功') return 'login success' # login ,fixture 中的 autouse預設為False, # 測試函式呼叫 login 可得到返回值 def test_login_1(login): print('\n返回值為:{0}'.format(login)) print('\n========== test_login_1 =========') # login ,fixture 中的 autouse預設為False, # 測試函式沒有呼叫 login def test_login_2(): print('\n========== test_login_2 =========')View Code
執行結果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py Testing started at 10:17 ... Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/dd/test_dd.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd ============================= test session starts ============================= platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe cachedir: .pytest_cache rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\dd collecting ... collected 2 items test_dd.py::test_login_1 登入成功 PASSED [ 50%] 返回值為:login success ========== test_login_1 ========= test_dd.py::test_login_2 PASSED [100%] ========== test_login_2 ========= ============================== 2 passed in 0.01s ============================== Process finished with exit code 0View Code