前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest配置文件pytest.ini

Pytest配置文件pytest.ini

原创
作者头像
霍格沃兹测试开发Muller老师
发布2024-05-14 17:39:36
960
发布2024-05-14 17:39:36
举报

pytest.ini 配置

pytest.ini 是什么

  • pytest.ini 是 pytest 的配置文件
  • 可以修改 pytest 的默认行为
  • 不能使用任何中文符号,包括汉字、空格、引号、冒号等等

pytest.ini

  • 修改用例的命名规则
  • 配置日志格式,比代码配置更方便
  • 添加标签,防止运行过程报警告错误
  • 指定执行目录
  • 排除搜索目录

pytest 配置- 改变运行规则

代码语言:python
复制
# 执行check_开头和 test_开头的所有的文件,后面一定要加*
python_files = check_* test_*
# 执行所有的以Test和Check开头的类
python_classes = Test*  Check*
# 执行所有以test_和check_开头的方法
python_functions= test_* check_*

pytest 配置- 添加默认参数

代码语言:python
复制
addopts = -v -s --alluredir=./results

添加默认参数后,输入pytest后,相当于下面这样直接输入一行,更方便

pytest 配置- 指定/忽略执行目录

代码语言:python
复制
# 设置执行的路径
testpaths = bilibili baidu
# 忽略某些文件夹/目录
norecursedirs = result logs datas test_demo*

pytest 配置- 日志

配置参考链接:pytest logging 收集日志

pytest.ini 文件配置日志级别,保存地址等内容。

  • 注意:undefinedwindows系统 需要把中文 注释去掉。
代码语言:python
复制
[pytest]
# 日志开关 true false
log_cli = true

# 输出到terminal
# 日志级别
log_cli_level = info
# 打印详细日志,相当于命令行加 -vs
addopts = --capture=no
# 日志格式
log_cli_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
# 日志时间格式
log_cli_date_format = %Y-%m-%d %H:%M:%S

# 输出到文件,log文件需要手动创建
# 日志文件位置
log_file = ./log/test.log
# 日志文件等级
log_file_level = info
# 日志文件格式
log_file_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
# 日志文件日期格式
log_file_date_format = %Y-%m-%d %H:%M:%S
代码语言:python
复制
# 可以动态生成log文件的名称,不过需要的pytest版本比较高
@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    log_name = 'output/log/' + now + '.logs'

    request.config.pluginmanager.get_plugin("logging-plugin") \
        .set_log_path(return_path(log_name))

总结 pytest.ini

  • 修改用例的命名规则
  • 配置日志格式,比代码配置更方便
  • 指定执行目录
  • 排除搜索目录
  • 添加标签,防止运行过程报警告错误
  • 添加默认参数

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • pytest.ini 配置
    • pytest.ini 是什么
      • pytest.ini
        • pytest 配置- 改变运行规则
          • pytest 配置- 添加默认参数
            • pytest 配置- 指定/忽略执行目录
              • pytest 配置- 日志
                • 总结 pytest.ini
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档