Pytest07-pytest.ini配置文件
1.pytest配置文件
固定名稱:pytest.ini
作用域:當前目錄及子目錄
具體配置功能見下:
[pytest]
# 01 把命令行參數自動添加到這里
addopts = -s -v --html=./report/report2023.html
# 02 指定執行的用例的目錄
testpaths = ./script
# 03 修改默認的測試文件名規則
python_files = auto*.py
# 04 修改默認的類命名規則
python_classes = Auto* B* C*
# 05 測試函數/方法的命名規則
python_functions = auto*
pytest.ini文件中如果有中文,可能會出編碼問題,需要把文件設置為gbk
1.選擇pytest.ini文件
2.點擊 file選項
3.選擇 File Encoding后
4.選擇 GBK
5.選擇convert
2.pytest常用插件
插件列表網址:https://docs.pytest.org/en/latest/reference/plugin_list.html
pytest-html插件
安裝:
pip install pytest-html
使用方法: 命令行格式:pytest --html=用戶路徑/report.html
3. 代碼案例
代碼目錄結構

文件:pytest.ini
[pytest]
# 01 把命令行參數自動添加到這里
addopts = -s -v --html=./report/report2023.html
# 02 指定執行的用例的目錄
testpaths = ./doc
# 03 修改默認的測試文件名規則
python_files = auto*.py
# 04 修改默認的類命名規則
python_classes = Auto* B* C*
# 05 測試函數/方法的命名規則
python_functions = auto*
文件:test_case01.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 作者:扶搖
import pytest
def test01():
print('用例一')
if __name__ == '__main__':
pytest.main()
文件:doc/auto01.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 作者:扶搖
def test01():
print("test01")
def auto01():
print("auto")
class B:
def test03(self):
print("test03")
文件:doc/test_aabb.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 作者:扶搖
# 測試函數
def test02():
print("doc目錄下的用例test02")
class TestShopping:
# pytest測試類不能使用構造方法
# def __init__(self):
# print("構造方法")
# 測試方法
def test03(self):
print("類下的用例")
測試技術交流請聯系我

備注博客園扶搖
【學習軟件測試/Python自動化測試技術/領取Python自動化測試學習路線圖/簡歷優化】
視頻鏈接:
課程服務介紹
加微信(備注博客園扶搖)即可免費領取下面的自動化測試資料和一份軟件測試面試寶典


本文來自博客園,作者:測試老宅男扶搖,轉載請注明原文鏈接:http://www.rzrgm.cn/cekailsf/p/17947559
浙公網安備 33010602011771號