18-pytest-配置文件pytest.ini使用

Posted 爱学习de测试小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了18-pytest-配置文件pytest.ini使用相关的知识,希望对你有一定的参考价值。

目录

前言

查看选项

使用方式

常用配置项

注意事项


前言

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

查看选项

  • pytest  --help
[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
  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

使用方式

  • 固定命名:pytest.ini (不要改名字)
  • 存放位置:放在项目根目录下

常用配置项

  • markers:注册自定义标签,主要解决自定义标记的warnings提示信息。
  • xfail_strict:让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败
  • addops:命令行参数
  • log_cli:控制台输出日志
  • norecursedirs:收集测试用例时,跳过的目录

# pytest.ini 

[pytest]

# 命令行参数 -v 详细打印 -s 打印调试信息   失败重跑两次   一共运行两次
addopts = -v -s --rerun=2 --count=2

# 注册mark
markers=
    web: web 测试用例
    app: app 测试用例

# 指定测试case路径
testpaths = testcase/

# 忽略的搜索路径
norecursedirs = evn reports/

# 搜索用例规则,测试类、测试文件、测试方法
python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*

# 控制台输出日志
log_cli=1

# 设置控制台日志信息
log_cli_level=info
log_cli_date_format=%Y-%m-%d-%H-%M-%S
log_cli_format=%(asctime)s-%(filename)s-%(module)s-%(funcName)s-%(lineno)d-%(levelname)s-%(message)s

# 设置日志文件信息
log_file=test.log
log_file_level=INFO
log_file_date_format=%Y-%m-%d %H:%M:%S
log_file_format=%(asctime)s %(filename)s %(module)s %(funcName)s %(lineno)d %(levelname)s: %(message)s

注意事项

  • pytest.ini 文件里面不能使用任何中文符号,包括汉字、空格、引号、冒号等;

以上是关于18-pytest-配置文件pytest.ini使用的主要内容,如果未能解决你的问题,请参考以下文章

pytest---pytest.ini配置文件

pytest-18-配置文件pytest.ini

pytest文档18-配置文件pytest.ini

pytest配置文件pytest.ini

pytest 配置文件

Selenium64-pytest.ini