幾個常用裝飾器
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
addopts: OPTS 命令行參數集
-s:表示輸出調試信息,包括 print打印的信息
-v:顯示更詳細的信息
-vs:這兩個參數一起用
-n :支持多線程或者分布式運行測試用例
如:pytest -vs ./testcase/test_login.py -n 2
--html : 測試報告位置
--reruns : 失敗重跑
-p no:warnings : 取消警告
--ff : 先執行上次失敗的用例
--lf : 只執行上次失敗的用例
-x : 遇到測試用例fail,就結束測試
--maxfail=num:遇到num條測試用例fail, 就結束測試
-k :根據測試用例的部分字符串指定測試用例
如:pytest -vs ./testcase -k “ao”
skipif-跳過測試
跳過測試的使用方法
pytest.skip (用于函數內,跳過測試用例)
def test_2():
if 1 2:
pytest.skip('1111111')
pass
@pytest.mark.skip(用于函數外,跳過測試用例)
@pytest.mark.skip(reason='feature not implemented')
def test_1():
pass
# 模塊級別跳過。(注:參數allow_module_level的值設為True)
pytest.skip('skip all tests', allow_module_level=True)
@pytest.mark.skipif(用于函數外,條件condition,跳過原因reason="xxx")
@pytest.mark.skipif(condition='12',reason='feature not implemented')
def test_1():
pass
ordering-執行順序
- 控制用例執行順序的方法
- 在需要調整用例執行順序的函數(或方法)前增加
@pytest.mark.run(order=x) x表示
3.數字數字形式: 小數、整數、負數
執行順序:
1、由小到大
2、由正到負
3、未標記 的在正數后,負數前執行
順序: 1,2,3,無標記,-3,-2,-1
xfail-預期失敗
xfail-預期失敗的函數
語法
xfail(condition, reason)
--condition 預期失敗的條件
--reason 預期失敗的原因
pytest.ini加參數,
不希望出現 預期失敗結果成功 的情況
就在配置文件中添加一個參數:
xfail_strict = true
fixture-函數作參數
fixture用途:可將被fixture標記的函數當作參數使用
掌握一個fixture 實現 setup 和 tearduwn
yield 關鍵字
yield 后邊代碼是用例執行完后再執行的。相當于teardown
當用例執行完之后, 會執行yield 后面的代碼,但不能 return
addfinalize 關鍵字
這個實現功能跟yield的一樣, 但可以用return,將參數傳給后面的用例.
fixture 可放到conftest.py文件下
conftest.py會自動識別 哪個用例調用了這個函數
parametrize-參數化
parametrize(argnames,argvalues)
--argnames : 參數名
--argvalues : 參數值, 數據類型是 list
語法
@pytest.mark.parametrize
@pytest.mark.parametrize("mobile,code", [(121,212),(123,321)])
rerunfailure-失敗重跑
失敗重跑機制
安裝pytest-rerunfailure
在設置文件pytest.ini中添加命令
reruns = 重跑次數
addopts = --reruns=10
鏈接Mysql
一.環境搭建
對接mysql數據庫需要通過第三方庫PyMySQl
二.數據庫操作
建立數據庫連接 :MySQlconnect = pymysql.connect(“數據庫地址“,“數據庫端口“,”數據庫賬號“等)
獲取操作游標: cursor = MySQlconnect .cursor()
執行SQL語句:cursor .execute(“SQL語句”)
獲取一條數據:data = cursor.fetchone()
獲取結果(讀):cursor.fetchall()
提交更改(寫):MySQlconnect .commit()
關閉游標:cursor.close()
關閉連接 :MySQlconnect .close()
到此這篇關于Python pytest裝飾器總結的文章就介紹到這了,更多相關pytest裝飾器內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python pytest進階之fixture詳解
- 詳解Pytest測試用例的執行方法
- python pytest進階之conftest.py詳解
- Pytest接口自動化測試框架搭建模板
- pytest自動化測試fixture的作用域實例化順序及可用性