目錄
1、pytest-metadata(元資料)
1.1、安裝
1.2、查看元資料
1.3、添加元資料
1.4、訪問元資料
2、pytest-base-url(可選擇URL地址)
2.1、安裝
2.2、示例
2.2.1、使用命令列
2.2.2、使用組態檔
3、pytest-instafail(實時輸出錯誤資訊)
3.1、安裝
3.2、示例
4、pytest-rerunfailures(失敗重試)
4.1、安裝
4.2、操作引數
4.2.1、重新運行所有失敗的用例
4.2.2、重新運行指定的測驗用例
4.3、兼容性
1、pytest-metadata(元資料)
元資料是關于資料的描述,存盤著關于資料的資訊,為人們更方便地檢索資訊提供了幫助,
pytest 框架里面的元資料可以使用 pytest-metadata 插件實作,
1.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-metadata
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-metadata -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
1.2、查看元資料
使用 pytest 執行用例的時候加上引數 --verbose,在控制臺輸出報告的頭部就會輸出元資料 (metadata) 資訊,
例如執行test_Demo用例:
pytest --verbose test_Demo.py
控制臺輸出的資訊,元資料是以鍵值對 (key-value) 方式存盤的:
wangmeng@wangmengdeMacBook-Pro My_pytest_Demo % pytest --verbose test_Demo.py
=========================================== test session starts ============================================
platform darwin -- Python 3.7.9, pytest-6.2.2, py-1.9.0, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
cachedir: .pytest_cache
metadata: {'Python': '3.7.9', 'Platform': 'Darwin-20.4.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'assume': '2.4.2', 'allure-pytest': '2.8.19', 'Faker': '8.6.0', 'metadata': '1.10.0', 'rerunfailures': '9.1.1', 'html': '3.0.0', 'xdist': '2.1.0', 'ordering': '0.6', 'cov': '2.10.1', 'repeat': '0.9.1', 'forked': '1.3.0'}, 'JAVA_HOME': '/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home'}
rootdir: /Users/wangmeng/PycharmProjects/My_pytest_Demo
plugins: assume-2.4.2, allure-pytest-2.8.19, Faker-8.6.0, metadata-1.10.0, rerunfailures-9.1.1, html-3.0.0, xdist-2.1.0, ordering-0.6, cov-2.10.1, repeat-0.9.1, forked-1.3.0

1.3、添加元資料
1、在命令列用 --metadata 引數添加鍵值對 (key, value) 的元資料,
例如:
pytest --metadata author Meng
2、如需添加多個元資料,可以使用多次 --metadata 引數添加,
例如:
pytest --metadata author Meng --metadata version v1.0
3、還可以將元資料作為 JSON 字串傳入
例如:
pytest --metadata-from-json '{"cat_says": "bring the cat nip", "human_says": "yes kitty"}'
1.4、訪問元資料
1、要在元資料收集結束時添加/修改/洗掉元資料,可以使用 hook 函式 pytest_metadata,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
@pytest.mark.optionalhook
def pytest_metadata(metadata):
metadata.pop("password", None)
2、要從測驗用例或 fixture 訪問元資料,可以使用元資料 metadata fixture,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_metadata(metadata):
assert 'metadata' in metadata['Plugins']
3、要從插件訪問元資料,可以使用 config 物件的 _metadata 屬性,這可用于讀取/添加/修改元資料,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def pytest_configure(config):
if hasattr(config, '_metadata'):
config._metadata['foo'] = 'bar'
關于 pytest-metadata 插件更詳細的使用,可查看官方網址:
https://pypi.org/project/pytest-metadata/#description
2、pytest-base-url(可選擇URL地址)
在自動化測驗執行中,我們可能會根據不同的環境(測驗環境、預發環境、生產環境等)來執行用例,需要把URL地址單獨提取出來,希望通過命令列或組態檔來執行,
pytest-base-url 是 pytest 插件,它通過命令列或組態檔提供一個可選的URL地址,
2.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-base-url
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-base-url -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2.2、示例
pytest-base-url 提供了一個 base_url 固定裝置,它回傳指定的基本URL地址,
創建test_base_url.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_case(base_url):
print("URL地址:" + base_url)
assert "https://www.cnblogs.com/alltests/" == base_url
2.2.1、使用命令列
命令列執行時,加上引數 --base-url
例如:
pytest -s test_base_url.py --base-url https://www.cnblogs.com/alltests/
運行結果:

2.2.2、使用組態檔
pytest.ini 組態檔里添加 base_url 地址
專案根目錄下,創建pytest.ini組態檔
組態檔內容:
[pytest]
base_url = https://www.cnblogs.com/alltests/
打開命令列執行(不需要加上引數 --base-url)
pytest -s test_base_url.py
運行結果:

3、pytest-instafail(實時輸出錯誤資訊)
pytest 執行全部用例的時候,如果有用例失敗,不會實時在控制臺列印出來,而是要等到全部用例執行完成后,才會把所有的報錯資訊一起拋到控制臺上顯示,不方便實時查看報錯資訊,
pytest-instafail 插件可以在運行用例的時候,如用例失敗,可實時查看報錯資訊,
使用引數 --instafail
3.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-instafail
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-instafail -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
3.2、示例
創建test_instafail.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_case1():
assert 1 == 1
def test_case2():
assert 0 == 1
創建test_instafail2.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
def test_case3():
assert 0 == 0
def test_case4():
assert 1 == 1
1、默認執行
打開命令列執行:
pytest test_instafail.py test_instafail2.py
運行結果:
用例全部執行完后,報錯資訊才拋出來,

2、使用引數 --instafail
打開命令列執行:
pytest test_instafail.py test_instafail2.py --instafail
運行結果:
用例執行程序中,有失敗用例,則實時拋出失敗資訊,

4、pytest-rerunfailures(失敗重試)
4.1、安裝
在命令列中運行以下命令進行安裝:
pip install pytest-rerunfailures
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快,)
pip install pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
4.2、操作引數
-
命令列引數:--reruns n(重新運行次數),--reruns-delay m(等待運行秒數)
-
裝飾器引數:reruns=n(重新運行次數),reruns_delay=m(等待運行秒數)
4.2.1、重新運行所有失敗的用例
創建test_rerunfailures.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
def test_case1():
print("\n==== 執行 test_case1 ====")
assert 1 == 1
@pytest.fixture()
def fixturefun():
print("\n==== 執行 fixturefun ====")
assert 2 == 2
return 3
def test_case2(fixturefun):
print("\n==== 執行 test_case2 ====")
assert fixturefun == 4
class TestCase():
def setup_class(self):
print("\n==== 執行 setup_class ====")
assert 4 == 5
def teardown_class(self):
print("\n==== 執行 teardown_class ====")
assert 5 == 6
def test_case3(self):
print("\n==== 執行 test_case3 ====")
assert 6 == 6
4.2.1.1、重新運行次數
要重新運行所有測驗失敗的,使用 --reruns 命令列選項,并指定要運行測驗的最大次數,
例如:
pytest test_rerunfailures.py --reruns 2 -s
運行結果:
設定失敗最大重新執行次數為2次
函式test_case2執行失敗后,又重新執行2次,同時參考的fixture也重新執行2次,
由于setup_class報錯,重新執行還是報錯,則類方法test_case3沒有被執行,

4.2.1.2、等待運行秒數
要在兩次重試之間增加延遲時間,使用 --reruns-delay 命令列選項,指定下次測驗重新開始之前等待的秒數,
例如:
pytest test_rerunfailures.py --reruns 2 --reruns-delay 5 -s
兩次重試之間等待5秒,
4.2.2、重新運行指定的測驗用例
要將單個測驗用例添加 flaky 裝飾器 @pytest.mark.flaky(reruns=n) ,并在測驗失敗時自動重新運行,需要指定最大重新運行的次數n,
創建test_rerunfailures2.py檔案
腳本代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公眾號:AllTests軟體測驗
"""
import pytest
@pytest.mark.flaky(reruns=2)
def test_case1():
print("\n==== 執行 test_case1 ====")
assert 1 == 2
@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_case2():
print("\n==== 執行 test_case2 ====")
assert 3 == 4
4.2.2.1、重新運行指定的測驗用例
函式test_case1、test_case2添加flaky裝飾器@pytest.mark.flaky(reruns=n)
運行結果:
函式test_case1設定失敗最大重新執行次數為2次

函式test_case2設定失敗最大重新執行次數為5次

注意:
如果指定了用例的重新運行次數,則在命令列執行添加 --reruns n 對這些用例是不會生效的,
例如:在命令列輸入命令
pytest test_rerunfailures2.py --reruns 1 -s
執行時,并沒有按照失敗最大重新執行次數設定的1次,還是按照指定用例里的設定,函式test_case1設定失敗最大重新執行次數為2次,函式test_case2設定失敗最大重新執行次數為5次,
4.2.2.2、等待運行秒數
在裝飾器里添加引數 reruns_delay=m(等待運行秒數)
例如:函式test_case2添加裝飾器@pytest.mark.flaky(reruns=5, reruns_delay=2)
兩次重試之間等待2秒,
4.3、兼容性
此插件不能與裝飾器 @pytest.fixture() 級別為(class, module, package)一起使用,
此插件與 pytest-xdis 的 –looponfail 標志不兼容,
此插件與 core –pdb 標志不兼容,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/339120.html
標籤:其他
