【pytest】pytest.ini配置文件(markers,addopts)

  • pytest.ini是pytest框架的主配置文件,实际生产中主要用来规范日志的格式或日志文件保存的位置,增加用例标签等等,总之简单易用,属于pytest学习的重要知识点。
  • pytest.ini文件命名不能修改,文件中第一行必须用【pytest】申明这是一个pytest的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 directories are given in the command line.
  filterwarnings (linelist):
                        Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
  usefixtures (args):   list of default fixtures to be used with this project
  python_files (args):  glob-style file patterns for Python test module discovery
  python_classes (args):
                        prefixes or glob names for Python test class discovery
  python_functions (args):
                        prefixes or glob names for Python test function and method discovery
  disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
                        disable string escape non-ascii characters, might cause unwanted side effects(use at your own risk)
  console_output_style (string):
                        console output: "classic", or with additional progress information ("progress" (percentage) | "count").
  xfail_strict (bool):  default for the strict parameter of xfail markers when not given explicitly (default: False)
  enable_assertion_pass_hook (bool):
                        Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache files.
  junit_suite_name (string):
                        Test suite name for JUnit report
  junit_logging (string):
                        Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|all
  junit_log_passing_tests (bool):
                        Capture log information for passing tests to JUnit report:
  junit_duration_report (string):
                        Duration time to report: one of total|call
  junit_family (string):
                        Emit XML for schema: one of legacy|xunit1|xunit2
  doctest_optionflags (args):
                        option flags for doctests
  doctest_encoding (string):
                        encoding used for doctest files
  cache_dir (string):   cache directory path.
  log_level (string):   default value for --log-level
  log_format (string):  default value for --log-format
  log_date_format (string):
                        default value for --log-date-format
  log_cli (bool):       enable log display during test run (also known as "live logging").
  log_cli_level (string):
                        default value for --log-cli-level
  log_cli_format (string):
                        default value for --log-cli-format
  log_cli_date_format (string):
                        default value for --log-cli-date-format
  log_file (string):    default value for --log-file
  log_file_level (string):
                        default value for --log-file-level
  log_file_format (string):
                        default value for --log-file-format
  log_file_date_format (string):
                        default value for --log-file-date-format
  log_auto_indent (string):
                        default value for --log-auto-indent
  faulthandler_timeout (string):
                        Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish.
  addopts (args):       extra command line options
  minversion (string):  minimally required pytest version
  required_plugins (args):
                        plugins that must be present for pytest to run
  base_url (string):    base url for the application under test.
  render_collapsed (bool):
                        Open the report with all rows collapsed. Useful for very large reports
  max_asset_filename_length (string):
                        set the maximum filename length for assets attached to the html report.
  rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.
  rsyncignore (pathlist):
                        list of (relative) glob-style paths to be ignored for rsyncing.
  looponfailroots (pathlist):
                        directories to check for changes
  • 自定义mark标签

    • 我门在编写自动化测试用例时,会有各种类型的场景用例,我们又不想一次性运行全部,只想运行其中的几个,这时我们可以借助mark标签来管理测试用例,mark标签是任意取的,但是要避开Python和pytest关键字,运行标签用 - m 来运行,如:pytest -m smoke
  • 自定义运行时的默认参数

    • pytest有多个经常使用的参数,如 - vs 来打印更详细的信息,--html=report.html --self-contained-html输出测试报告等。但是每次都要手动输入很麻烦,我们可不可设置成默认详细打印呢?这是需要用到 addopts 配置默认运行参数
    • 参数详解
      • -s 详细输出
      • -v 输出具体运行的case
      • -q 简要输出
      • -m 指定标签(pytest.mark.标签)
      • -k 模糊匹配,测试用例的部分字符串,指定执行测试用例
      • -x:表示只要有一个测试用例报错,则执行停止
      • –maxfail=2:表示出现2个用例报错,则执行停止
      • –reruns X:失败用例重跑,跑几次
    • addopts 运行时参数(可添加多个命令行参数,空格分隔,所有参数与命令行一致)
  • 自定义用例规则

    • pytest 默认查找用例匹配规则
      • 测试文件以test_开头(以_test结尾也可以)
      • 测试类以Test开头,并且不能带有 init 方法
      • 测试函数以test_开头
    • 想要修改用例匹配规则直接在pytest.ini中直接配置即可
      • python_files = test_*.py *_test.py 模块名的规则
      • python_classes = Test* 类名的规则
      • python_functions = test_* 方法或者函数的规则
posted @ 2022-08-12 21:56  Tony_xiao  阅读(1202)  评论(0编辑  收藏  举报