pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序
pytest fixtures裝飾器
pytest中可以使用@pytest.fixture 裝飾器來裝飾一個方法,被裝飾方法的方法名可以作為一個參數(shù)傳入到測試方法中??梢允褂眠@種方式來完成測試之前的初始化,也可以返回數(shù)據(jù)給測試函數(shù)。
將fixture作為函數(shù)參數(shù)
通常使用setup和teardown來進行資源的初始化,如果有這樣一個場景,測試用例1需要依賴登入功能,測試用例2不需要依賴登入功能,測試用例3需要登入功能,這種場景setup,teardown無法實現(xiàn),也可以使用pytest fixture功能,在這個方法前面加個@pytest.fixture裝飾器,加了這個裝飾器的方法可以以參數(shù)的形式傳到方法里,這個方法就會先執(zhí)行這個登入方法,再去執(zhí)行自身的用例步驟,如果沒有傳入這個登入方法就不執(zhí)行登入操作,直接執(zhí)行已有的步驟
#!/usr/bin/env python # _*_coding: utf-8 _*_ import pytest @pytest.fixture() def login(): print("這時一個登入的方法") return ('tome', '123') @pytest.fixture() def operate(): print("這是登入后的操作") def test_case1(login, operate): print(login) print("test_case1,需要登入") def test_case2(): print("test_case2,不需要登入") def test_case3(login): print(login) print("test_case3,需要登入")
在上面的代碼中,測試用例test_case1 和test_case3 分別增加了login 方法名作為參數(shù),pytest會發(fā)現(xiàn)并調(diào)用@pytest.fixture標記的login功能,運行測試結(jié)果如下:
Testing started at 10:17 ... C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture.py Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123 ============================= test session starts ============================= platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123 plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items test_fixture.py 這時一個登入的方法 這是登入后的操作 .('tome', '123') test_case1,需要登入 .test_case2,不需要登入 這時一個登入的方法 .('tome', '123') test_case3,需要登入 [100%] ============================== 3 passed in 0.04s ============================== Process finished with exit code 0
從上面結(jié)果可以看出,test_case1 和test_case3 運行之前執(zhí)行了login方法,test_case2沒有執(zhí)行這個方法。
控制用例的執(zhí)行順序
一、pytest加載所有的用例都是亂序的,如果想指定用例的順序,可以使用pytest-ordering插件,指定用例的執(zhí)行順序只需要在測試用例的方法前面加上裝飾器@pytest.mark.run(order=[num])設(shè)置order的對應(yīng)的num值,它就可以按照num的大小順序來執(zhí)行。
應(yīng)用場景:有時運行測試用例要指定它的順序,比如有些場景要先需要登入,才能執(zhí)行后面的流程比如購物流程,下單流程,這時就需要指定用例的執(zhí)行順序。通過pytest-ordering這個插件可以完成用例順序的指定。
二、安裝
pip install pytest-ordering
三、實例
#!/usr/bin/env python # _*_coding: utf-8 _*_ import pytest class Testpytest(object): @pytest.mark.run(order=-1) def test_two(self): print("test_two, 測試用例") @pytest.mark.run(order=3) def test_one(self): print("test_one, 測試用例") @pytest.mark.run(order=1) def test_three(self): print("test_three, 測試用例")
四、運行結(jié)果
Testing started at 15:51 ... C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123 ============================= test session starts ============================= platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123 plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items test_order.py [100%] ============================== 3 passed in 0.06s ============================== Process finished with exit code 0 .test_three, 測試用例 .test_one, 測試用例 .test_two, 測試用例
以上就是pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序的詳細內(nèi)容,更多關(guān)于pytest fixtures裝飾器和控制用例的執(zhí)行順序的資料請關(guān)注腳本之家其它相關(guān)文章!
- pytest解讀fixtures中yield與addfinalizer區(qū)別
- pytest解讀fixtures之Teardown處理yield和addfinalizer方案
- pytest官方文檔解讀fixtures的調(diào)用方式
- pytest官方文檔解讀fixtures
- pytest官方文檔解讀fixtures的autouse
- pytest解讀一次請求多個fixtures及多次請求
- pytest官方文檔解讀fixtures調(diào)用fixtures及fixture復(fù)用性
- pytest解讀fixture有效性及跨文件共享fixtures
- pytest?fixtures函數(shù)及測試函數(shù)的參數(shù)化解讀
- Pytest中Fixtures的高級用法
相關(guān)文章
利用Python?實現(xiàn)圖片轉(zhuǎn)字符畫
這篇文章主要介紹了利用Python?實現(xiàn)圖片轉(zhuǎn)字符畫,要將圖片轉(zhuǎn)字符畫,需要先定義一個字符集,用來和灰度值做映射,將圖片每個像素的?RGB?值轉(zhuǎn)換為一個灰度值,將其對應(yīng)的字符輸出就得到字符畫2022-06-06Python實現(xiàn)從網(wǎng)絡(luò)攝像頭拉流的方法分享
這篇文章主要為大家詳細介紹了Python實現(xiàn)從網(wǎng)絡(luò)攝像頭拉流的幾種方法,文中的示例代碼講解詳細,具有一定的學(xué)習價值,感興趣的小伙伴可以了解一下2023-01-01Pytorch之tensorboard無法啟動和顯示問題及解決
這篇文章主要介紹了Pytorch之tensorboard無法啟動和顯示問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Python找出列表中出現(xiàn)次數(shù)最多的元素三種方式
本文通過三種方式給大家介紹Python找出列表中出現(xiàn)次數(shù)最多的元素,每種方式通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下2020-02-02Python 解析pymysql模塊操作數(shù)據(jù)庫的方法
這篇文章主要介紹了Python 解析pymysql模塊操作數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02Python統(tǒng)計某列不同值的個數(shù)的示例代碼
在數(shù)據(jù)分析和數(shù)據(jù)處理中,統(tǒng)計數(shù)據(jù)往往集中在特定列中不同值的出現(xiàn)次數(shù),本文主要介紹了Python統(tǒng)計某列不同值的個數(shù)的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-03-03