pytest.ini 配置文件 例子:
[pytest] addopts = -v -s --html=py_test/scripts/report/report.html -p no:warnings --reruns=10 testpaths = ./py_test/scripts python_files= test_rerun.py python_classes = Test* python_function = test* xfail_strict = true
-s:表示輸出調(diào)試信息,包括 print打印的信息 -v:顯示更詳細的信息 -vs:這兩個參數(shù)一起用 -n :支持多線程或者分布式運行測試用例 如:pytest -vs ./testcase/test_login.py -n 2 --html : 測試報告位置 --reruns : 失敗重跑 -p no:warnings : 取消警告 --ff : 先執(zhí)行上次失敗的用例 --lf : 只執(zhí)行上次失敗的用例 -x : 遇到測試用例fail,就結(jié)束測試 --maxfail=num:遇到num條測試用例fail, 就結(jié)束測試 -k :根據(jù)測試用例的部分字符串指定測試用例 如:pytest -vs ./testcase -k “ao”
跳過測試的使用方法
pytest.skip (用于函數(shù)內(nèi),跳過測試用例)
def test_2(): if 1 2: pytest.skip('1111111') pass
@pytest.mark.skip(用于函數(shù)外,跳過測試用例)
@pytest.mark.skip(reason='feature not implemented') def test_1(): pass # 模塊級別跳過。(注:參數(shù)allow_module_level的值設(shè)為True) pytest.skip('skip all tests', allow_module_level=True)
@pytest.mark.skipif(用于函數(shù)外,條件condition,跳過原因reason="xxx")
@pytest.mark.skipif(condition='12',reason='feature not implemented') def test_1(): pass
@pytest.mark.run(order=x) x表示
3.數(shù)字?jǐn)?shù)字形式: 小數(shù)、整數(shù)、負數(shù)
執(zhí)行順序:
1、由小到大
2、由正到負
3、未標(biāo)記 的在正數(shù)后,負數(shù)前執(zhí)行
順序: 1,2,3,無標(biāo)記,-3,-2,-1
xfail-預(yù)期失敗的函數(shù)
語法
xfail(condition, reason)
--condition 預(yù)期失敗的條件
--reason 預(yù)期失敗的原因
不希望出現(xiàn) 預(yù)期失敗結(jié)果成功 的情況
就在配置文件中添加一個參數(shù):
xfail_strict = true
fixture用途:可將被fixture標(biāo)記的函數(shù)當(dāng)作參數(shù)使用
掌握一個fixture 實現(xiàn) setup 和 tearduwn
yield 關(guān)鍵字
yield 后邊代碼是用例執(zhí)行完后再執(zhí)行的。相當(dāng)于teardown
當(dāng)用例執(zhí)行完之后, 會執(zhí)行yield 后面的代碼,但不能 return
addfinalize 關(guān)鍵字
這個實現(xiàn)功能跟yield的一樣, 但可以用return,將參數(shù)傳給后面的用例.
fixture 可放到conftest.py文件下
conftest.py會自動識別 哪個用例調(diào)用了這個函數(shù)
parametrize(argnames,argvalues)
--argnames : 參數(shù)名
--argvalues : 參數(shù)值, 數(shù)據(jù)類型是 list
語法
@pytest.mark.parametrize
@pytest.mark.parametrize("mobile,code", [(121,212),(123,321)])
失敗重跑機制
安裝pytest-rerunfailure
在設(shè)置文件pytest.ini中添加命令
reruns = 重跑次數(shù)
addopts = --reruns=10
對接mysql數(shù)據(jù)庫需要通過第三方庫PyMySQl
建立數(shù)據(jù)庫連接 :MySQlconnect = pymysql.connect(“數(shù)據(jù)庫地址“,“數(shù)據(jù)庫端口“,”數(shù)據(jù)庫賬號“等)
獲取操作游標(biāo): cursor = MySQlconnect .cursor()
執(zhí)行SQL語句:cursor .execute(“SQL語句”)
獲取一條數(shù)據(jù):data = cursor.fetchone()
獲取結(jié)果(讀):cursor.fetchall()
提交更改(寫):MySQlconnect .commit()
關(guān)閉游標(biāo):cursor.close()
關(guān)閉連接 :MySQlconnect .close()
到此這篇關(guān)于Python pytest裝飾器總結(jié)的文章就介紹到這了,更多相關(guān)pytest裝飾器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:雙鴨山 赤峰 貴州 日照 臨汾 金華 陽泉 克拉瑪依
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python pytest裝飾器總結(jié)(實例詳解)》,本文關(guān)鍵詞 Python,pytest,裝飾,器,總結(jié),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。