Pytest07-pytest.ini配置文件

发布时间 2024-01-05 16:33:29作者: 测试老宅男扶摇

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自动化测试学习路线图/简历优化】
视频链接:
课程服务介绍

自动化全栈学习路线图讲解

软件测试面试合集

Python编程刷题合集

Pytest入门到实战

Python接口自动化合集

PythonWeb自动化合集

性能测试合集

Jmeter接口自动化测试实战全集

2023GPT探索发现合集

加微信(备注博客园扶摇)即可免费领取下面的自动化测试资料和一份软件测试面试宝典