pytest fixtures裝飾器
pytest中可以使用@pytest.fixture 裝飾器來裝飾一個方法,被裝飾方法的方法名可以作為一個參數傳入到測試方法中。可以使用這種方式來完成測試之前的初始化,也可以返回數據給測試函數。
將fixture作為函數參數
通常使用setup和teardown來進行資源的初始化,如果有這樣一個場景,測試用例1需要依賴登入功能,測試用例2不需要依賴登入功能,測試用例3需要登入功能,這種場景setup,teardown無法實現,也可以使用pytest fixture功能,在這個方法前面加個@pytest.fixture裝飾器,加了這個裝飾器的方法可以以參數的形式傳到方法里,這個方法就會先執行這個登入方法,再去執行自身的用例步驟,如果沒有傳入這個登入方法就不執行登入操作,直接執行已有的步驟
#!/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 方法名作為參數,pytest會發現并調用@pytest.fixture標記的login功能,運行測試結果如下:
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
從上面結果可以看出,test_case1 和test_case3 運行之前執行了login方法,test_case2沒有執行這個方法。
控制用例的執行順序
一、pytest加載所有的用例都是亂序的,如果想指定用例的順序,可以使用pytest-ordering插件,指定用例的執行順序只需要在測試用例的方法前面加上裝飾器@pytest.mark.run(order=[num])設置order的對應的num值,它就可以按照num的大小順序來執行。
應用場景:有時運行測試用例要指定它的順序,比如有些場景要先需要登入,才能執行后面的流程比如購物流程,下單流程,這時就需要指定用例的執行順序。通過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, 測試用例")
四、運行結果
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裝飾器的使用和如何控制用例的執行順序的詳細內容,更多關于pytest fixtures裝飾器和控制用例的執行順序的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:- Python 測試框架unittest和pytest的優劣
- python pytest進階之conftest.py詳解
- python pytest進階之fixture詳解
- python中pytest收集用例規則與運行指定用例詳解
- python的pytest框架之命令行參數詳解(上)
- Python pytest裝飾器總結(實例詳解)