柚子快報邀請碼778899分享:pytest基本應(yīng)用
柚子快報邀請碼778899分享:pytest基本應(yīng)用
文章目錄
1.pytest安裝
2.用例運行規(guī)則
3.常用參數(shù)
斷言
運行參數(shù)
用例控制
setup和teardown
ini配置文件
4.常用插件
5.pytest高階用法
用例跳過
參數(shù)化
6.pytest之Fixture使用
fixture使用
裝飾器usefixtures
7.pytest之conftest.py
8.conftest+fixture+yield
yield介紹
前后置使用
1.pytest安裝
pip install pytest
環(huán)境配置:需要設(shè)置項目的默認(rèn)運行方式為unittest
2.用例運行規(guī)則
pytest將在當(dāng)前目錄及其子目錄中運行所有格式為test_*.py開頭或*_test.py結(jié)尾的文件
測試方法/測試函數(shù) 默認(rèn)必須是test開頭
測試類必須是Test開頭
測試類不能有構(gòu)造方法 __init__
示例:
運行時可以掃描到當(dāng)前目錄和當(dāng)前子目錄下的用例:
3.常用參數(shù)
斷言
斷言借助Python中的運算符號和assert關(guān)鍵字實現(xiàn);
# -*- coding: utf-8 -*-
# @Time : 2024/2/19 17:56
# @Author : 居里夫人吃橘子
# @File : test_demo03.py
# @Software: PyCharm
import pytest
"""
測試不相等 !=
<=
>=
測試包含 in
測試不包含 not in
判斷是否為true: is True
判斷是否不為true: is not True/is False
and
or
"""
def test_demo03():
print('kkkkkkk')
# assert 1 == 1
# assert 1 != 2
# assert 1 < 2
# assert 1 > 2
# assert 'a' in 'abc'
# assert 'a' not in 'abc'
# assert True
# assert False
# assert 1 == 1 and 1 < 2
assert 1 == 2 or 1 < 2
if __name__ == '__main__':
pytest.main(['-s'])
運行參數(shù)
“-s” 參數(shù) 用于關(guān)閉捕捉,從而輸出打印信息到控制臺
“-v” 參數(shù) 用于顯示具體的用例執(zhí)行信息‘
“-k” 運行名稱中包含某字符串的測試用例,類似于模糊查找
‘-q’ 簡化輸出信息
‘-x’ 如果出現(xiàn)一條測試用例失敗,則退出測試
指定運行測試文件中的方法或特定的類
pytest.main([‘-s’, ‘./son_dir/test_son01.py::TestDemo01’])
用例控制
在第N個用例失敗后,結(jié)束測試;
pytest.main(['-s','test_demo03.py','--maxfail=1'])
失敗用例重跑
安裝插件pytest-rerunfailures,注意Python版本需要>3.5;pytest>5.0
pip install pytest-rerunfailures
# '--reruns'表示重跑次數(shù);'--reruns-delay'表示失敗后間隔幾秒
pytest.main(['--reruns', '3', '--reruns-delay', '1', 'test_demo03.py'])
通過標(biāo)記表達(dá)式執(zhí)行指定的用例
通過@pytest.mark.標(biāo)記名裝飾器標(biāo)記指定的測試用例,使用pytest.main(['-m', '標(biāo)記名'])執(zhí)行;
注:標(biāo)記名需要提前注冊,創(chuàng)建pytest.ini文件;標(biāo)記名稱冒號后面的為注釋,最好使用英文;
[pytest]
markers =
ma: marks tests as smoke
slow: slow testcase
@pytest.mark.slow
def test_demo05():
print('5555555555')
if __name__ == '__main__':
pytest.main(['-s', 'test_demo03.py', '-m', 'slow'])
多進(jìn)程執(zhí)行測試用例
安裝插件pytest-xdist;
pip install pytest-xdist
pytest.main(['-n', 'auto', 'test_demo03.py']) #auto為自動分配與計算機(jī)相匹配的進(jìn)程;也可以指定數(shù)字
setup和teardown
不含有類的用例前置和后置
1.setup_module/teardown_module: 在當(dāng)前文件中,在所有測試用例執(zhí)行之前與之后執(zhí)行 2.setup_function/teardown_function:在每個測試函數(shù)之前與之后執(zhí)行
# -*- coding: utf-8 -*-
# @Time : 2024/2/20 10:49
# @Author : 居里夫人吃橘子
# @File : test_set01.py
# @Software: PyCharm
# 功能函數(shù),相乘
import pytest
def multiply(a, b):
return a * b
"""
不含有類的用例預(yù)置和后置函數(shù)
第一批次:setup_module/teardown_module: 在當(dāng)前文件中,在所有測試用例執(zhí)行之前與之后執(zhí)行
第二批次:setup_function/teardown_function:在每個測試函數(shù)之前與之后執(zhí)行,不能在類中使用
ps:執(zhí)行的順序按優(yōu)先級來的,改變方法位置結(jié)果也一樣
"""
# 在當(dāng)前文件前置后置
def setup_module(module):
print("setup_module===================")
def teardown_module(module):
print("teardown_module================")
# 每個用例前置后置
def setup_function(function):
print("setup_function===================")
def teardown_function(function):
print("teardown_function================")
# ========測試用例=========
def test01():
print("第一個用例")
print(multiply(3, 4))
def test02
柚子快報邀請碼778899分享:pytest基本應(yīng)用
推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。