日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

接口自动化框架之python pytest-mark(三)

發(fā)布時(shí)間:2024/3/24 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 接口自动化框架之python pytest-mark(三) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、mark標(biāo)簽介紹

在測(cè)試用例/測(cè)試類前面加上:@pytest.mark.標(biāo)簽名,打標(biāo)記范圍:測(cè)試用例、測(cè)試類、模塊文件


二、使用mark進(jìn)行分類

在使用mark標(biāo)簽之前要?jiǎng)?chuàng)建pytest.ini配置文件,同樣在運(yùn)行的時(shí)候,‘-m’參數(shù)后邊也要標(biāo)識(shí)分類標(biāo)簽的名稱

1.創(chuàng)建測(cè)試代碼

import pytestdef test_01():print('oi')@pytest.mark.number_01 def test_02():print('iu')def test_03():print('iuu')

2.配置文件:(注意:一定是pytest.ini)

[pytest]markers=number_01: 分類1number_02: 分類2number_03: 分類3

3.執(zhí)行:

if __name__ == '__main__':pytest.main(['-s','-v','-m','number_01','test_mark.py'])

完整代碼:

import pytestdef test_01():print('oi')@pytest.mark.number_01 def test_02():print('iu')def test_03():print('iuu')if __name__ == '__main__':pytest.main(['-s','-v','-m','number_01','test_mark.py'])

三、mark參數(shù)化(基礎(chǔ)版,后續(xù)會(huì)單獨(dú)再去深入寫(xiě))

?@pytest.mark.parametrize(),括號(hào)內(nèi)寫(xiě)入?yún)?shù)名稱和參數(shù)值

舉例:

import pytestname_value = ['bob','yumi','xiaoming']@pytest.mark.parametrize('name',name_value) def test_01(name):print(name)if __name__ == '__main__':pytest.main(['-s','-v','test_mark.py'])

參數(shù)化:

use_info =[('tom','123'),('bob','2345'),('cindy','7823')] @pytest.mark.parametrize('username,pwd',use_info) def test_login(username,pwd):print(username +pwd)

四、設(shè)置執(zhí)行測(cè)試用例順序

1.安裝pytest -ordering

pip install pytest-ordering
2.參數(shù)order后邊數(shù)字越大,順序越靠前(默認(rèn)是按照書(shū)寫(xiě)的順序來(lái)執(zhí)行)

import pytest@pytest.mark.run(order=3) def test_01():print('01')@pytest.mark.run(order=2) def test_02():print('02')@pytest.mark.run(order=1) def test_03():print('03')if __name__ == '__main__':pytest.main(['-s','-v','test_mark.py'])

?

五、使用mark來(lái)標(biāo)記用例跳過(guò)

1.跳過(guò)

1.1無(wú)判斷直接跳過(guò):@pytest.mark.skip(reason ='跳過(guò)的原因')

1.2根據(jù)條件判斷跳過(guò):@pytest.mark.skipif(version<8,reason='版本過(guò)低')

import pytest@pytest.mark.skip(reason ='跳過(guò)的原因') def test_01():print('01')version = 4.0 @pytest.mark.skipif(version<8,reason='版本過(guò)低') def test_02():print('02')def test_03():print('03')if __name__ == '__main__':pytest.main(['-s','-v','test_mark.py'])

六、使用mark標(biāo)記超時(shí)時(shí)間

6.1安裝pytest-timeout插件,在我們做接口自動(dòng)化的時(shí)候,很經(jīng)常會(huì)有一個(gè)指標(biāo)是找出一些耗時(shí)的接口,從而告知開(kāi)發(fā)這些接口需要優(yōu)化

pip install?pytest-timeout

tips:可能是由于pytest版本過(guò)高,導(dǎo)致目前只能安裝timeouts插件,?pip install?pytest-timeout 更常用,但pip install pytest-timeouts?更強(qiáng)大

?6.2基于函數(shù)的超時(shí)設(shè)置

from time import sleep import pytest@pytest.mark.timeout(3) #設(shè)置超時(shí)時(shí)間為5s def test_01():sleep(5)print('01')def test_02():print('02')if __name__ == '__main__':pytest.main(['-s','-v','test_mark.py'])

總結(jié)

以上是生活随笔為你收集整理的接口自动化框架之python pytest-mark(三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。