首页 > 其他分享 >Pytest配置文件pytest.ini

Pytest配置文件pytest.ini

时间:2024-05-14 17:41:22浏览次数:17  
标签:-% log 配置文件 pytest Pytest ini test 日志

pytest.ini 配置

pytest.ini 是什么

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

pytest.ini

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

pytest 配置- 改变运行规则

# 执行check_开头和 test_开头的所有的文件,后面一定要加*
python_files = check_* test_*
# 执行所有的以Test和Check开头的类
python_classes = Test*  Check*
# 执行所有以test_和check_开头的方法
python_functions= test_* check_*

pytest 配置- 添加默认参数

addopts = -v -s --alluredir=./results

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

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

# 设置执行的路径
testpaths = bilibili baidu
# 忽略某些文件夹/目录
norecursedirs = result logs datas test_demo*

pytest 配置- 日志

配置参考链接:pytest logging 收集日志
pytest.ini 文件配置日志级别,保存地址等内容。

  • 注意:
    windows系统 需要把中文 注释去掉。
[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
# 可以动态生成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

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

标签:-%,log,配置文件,pytest,Pytest,ini,test,日志
From: https://www.cnblogs.com/hogwarts/p/18191830

相关文章

  • pytest 学习 - 03 fixture
    前言有的场景setup与teardown无法满足,如:有的用例需要登录才能执行,有的用例不需要登录。fixturefixture可以满足以上特殊的场景。1.只有登录的函数上面加上 @pytest.fixture()2. 在要使用的测试方法中传入(登录函数名称),就先登录案例[email protected]()def......
  • 一次grub配置文件的生成解释
    运行grub-mkconfig命令,需要到/usr/sbin目录运行,因为这个目录没有包含到PATH变量中。 执行命令,并输出内容jack@PC:/usr/sbin$sudo./grub-mkconfig-o/boot/grub/grub.cfgGeneratinggrubconfigurationfile...Foundbackgroundimage:/usr/share/images/desktop-b......
  • 【pytest】将测试用例内的数据回填给fixtrue 中
    【背景】每个测试用例生成对应的log文件,需要将测试脚本名称传给log对象,生成对于的log文件【分析】方法一:使用@pytest.mark.parametrize的 indirect=True ,参数1:执行对应的函数(fixtrue中的函数名称) 参数2:函数对应的参数  参考:Pytest系列(10)-fixture传参数reques......
  • pytest 学习 - 02 失败重新运行
    前言测试失败后要重新运行n次,要在重新运行之间添加延迟时间,间隔n秒再运行安装:pipinstallpytest-rerunfailures案例importpytestclassTestDemo:deftest_a(self):print("失败用例")assert1==2deftest_b(self):print("成功用......
  • pytest 学习 - 01 setup与teardown
    前言与unitest一样,python也拥有前置(setup)、后置操作(teardown)操作,而且更加的灵活setup与teardown介绍1.模块级别(setup_module/teardown_module)模块始末,全局的(优先最高)2.函数级(setup_function/teardown_function)只对函数用例生效(不在类中)3.类级(setup_class/teardown_c......
  • maven的配置文件的心得(profile和mirrorof)
    https://www.cnblogs.com/li1111xin/p/4777965.html1、maven安装后有两个地方有settings.xml,(1)一个是maven安装路径下:conf/settings.xml,(2)一个是用户的本地仓库下:.m2/settings.xml,这个路径是默认的用户的maven的本地仓库的路径(3)如果指定了自定义的本地仓库的路径,那么把setting......
  • 1-分库分表概述_工作过程_分片规则_目录结构_配置文件
    1.分库分表概述工作过程分片规则目录结构配置文件分库分表概述 工作过程 分片规则 目录结构 配置文件分库分表应用注意1.在数据库设计时考虑垂直分库和垂直分表2.数据量增加时,不要马上做水平分割,而是先考虑缓存处理、读写分离、使用索引等方式。如果以上方式不能解决,......
  • mybatis核心配置文件
    在resource目录下,创建mybatis-config.xml(官方建议起名方式)核心配置文件常用元素properties:通过resource属性从外部指定属性文件(db.properties),该属性文件描述数据库连接的相关配置(数据库驱动、连接数据库的url、数据库用户名、数据库密码),位置也是在/resources目录下settin......
  • pytest + yaml 框架 -69.新增depend 关键字,导入其它yaml用例
    前言有小伙伴提到,test_a.yml中已经写了几个流程,test_b.yml中希望能接着test_a.yml去写用例,于是就需要先导入test_a.yml用例。为了满足此需求,v1.6.3版本在config中新增depend关键字。需求场景test_a.yml中已经写了一些用例config:variables:x:helloy:wo......
  • 配置文件
    //1.QMetaObject::connectSlotsByNamevoidWidget::on_btnOpen_clicked(){QStringconf_path=qApp->applicationDirPath()+"/conf/settings.ini";std::unique_ptr<QSettings>iniSet(newQSettings(conf_path,QSettings::IniFormat))......