python + pytest基本使用方法(拓展庫)


一、測試鈎子配置文件

import pytest
# conftest.py 是pytest特有的本地測試配置文件;
# 既可以用來設置項目級別的Fixture,也可用來導入外部插件,還可以指定鈎子函數

#設置測試鈎子

@pytest.fixture()
def test_url():
return 'http://www.baidu.com'

二、測試失敗用例進行連跑
# pip install pytest-rerunfailures
# pytest-rerunfailures 可以在測試用例失敗時進行重試(連跑)

#通過 ‘--reruns’參數設置測試用例運行失敗后的重試次數
#執行 pytest -v test_rerunfailures.py --reruns 3

def test_fail_rerun():
assert 2 + 2 == 5

三、測試線程數
from time import sleep

# pip install pytest-parallel
# pytest-parallel 擴展可以實現測試用例的並行運行

# 不使用線程運行測試用例
# pytest -q test_parallel.py

# 參數‘--tests-per-worker’用來指定線程數,auto 表自動分配
#pytest -q test_parallel.py --tests-per-worker auto

def test_01():
sleep(3)

def test_02():
sleep(3)

def test_03():
sleep(6)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM