python-pytest配置文件pytest.ini

  1. pytest.ini 的作用
  2. pytest.ini 的配置内容
    1. ini文件基本格式(注意保存为pytest.ini文件)
    2. addopts 参数
    3. markers 参数
    4. testpaths、norecursedirs 参数
    5. 日志配置

pytest.ini 的作用

pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。

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

pytest.ini 的配置内容

ini文件基本格式(注意保存为pytest.ini文件)

[pytest]

addopts = -rsxX
xfail_strict = true

使用pytest –help指令可以查看pytest.ini的设置选项

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:

  markers (linelist)       markers for test functions
  empty_parameter_set_mark (string) default marker for empty parametersets
  norecursedirs (args)     directory patterns to avoid for recursion
  testpaths (args)         directories to search for tests when no files or dire

  console_output_style (string) console output: classic or with additional progr

  usefixtures (args)       list of default fixtures to be used with this project

  python_files (args)      glob-style file patterns for Python test module disco

  python_classes (args)    prefixes or glob names for Python test class discover

  python_functions (args)  prefixes or glob names for Python test function and m

  xfail_strict (bool)      default for the strict parameter of 
  addopts (args)           extra command line options
  minversion (string)      minimally required pytest version

–rsxX 表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因

addopts 参数

addopts参数可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,会用到,比如我想测试完生成报告,指令比较长

pytest -v --reruns 1 --html=report.html --self-contained-html

每次输入这么多,不太好记住,于是可以加到pytest.ini里

[pytest]

addopts = -v --reruns 1 --html=report.html --self-contained-html

markers 参数

在用例中,使用mark标记功能对于以后分类测试非常有用处,举例如下:

import pytest


@pytest.mark.androidtest
def test_open_android():
    print("------android用例------")


@pytest.mark.iostest
class TestCls:
    def test_01(self):
        print("------ios用例01------")

    def test_02(self):
        print("------ios用例02------")

if __name__ == "__main__":
    pytest.main(["-v", "test_sample.py", "-m=androidtest"])

执行用例时,可选择执行android用例还是ios用例

pytest -vs -m androidtest
pytest -vs -m iostest

有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件

[pytest]

markers =
  androidtest:  Run the android case
  iostest: Run the ios case

标记好之后,可以使用pytest –markers查看到所有的markers,便于记住相应标签

@pytest.mark.androidtest:  Run the android case

@pytest.mark.iostest: Run the ios case

...

testpaths、norecursedirs 参数

;设置用例搜索路径
testpaths = bilibili baidu
;忽略用例搜索路径
norecursedirs = result logs datas test_demo*

日志配置

;日志开关 true false
log_cli = true
;日志级别
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_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

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
My Show My Code