Believe in yourself.

Pytest学习笔记(六) pytest.ini配置文件

pytest.ini配置文件可以改变pytest一些默认的运行方式,如:用例收集规则,标签,命令行参数等等。

基本格式如下:

# 新建pytest.ini文件,一般放在项目的顶级目录下,不能随意命名

[pytest]

addopts = -v --rerun=2 --count=2
xfail_strict = true

使用pytest -h参看帮助文档,找到这行:ini-options in the first pytest.ini|tox.ini|setup.cfg file found

[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

以下是一些常用的配置:

1、addopts 

  更改默认命令行选项,当我们用命令行运行时,需要输入多个参数,很不方便。比如想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在命令行中执行的话,命令会很长,很不方便

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

这时可以在pytest.ini文件通过addopts 加上子而写参数

# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

在命令行中只需要输入pytest就可以默认以这些参数去执行了。

2、更改测试用例收集规则

pytest默认的测试用例收集规则

  • 文件名以 test_*.py 文件和 *_test.py
  • 以  test_ 开头的函数
  • 以  Test 开头的类,不能包含 __init__ 方法

如果需要修改这些规则,可以在pytest.ini文件中加入以下配置:

#测试用例收集规则
python_files =  test_*.py  *_test.py    # 文件名
python_classes = Test*                      #
python_functions = test_*                  # 函数

多个匹配规则以空格分开

3、指定搜索测试用例目录

testpaths = apicase     #指定用例搜索目录

只收集apicase目录下的测试用例

4、排除搜索目录

norecursedirs = tmp* *plugins

不搜索tmp的前缀文件和plugins的后缀文件

5、指定mark标签

markers =
    smoke: this is smoke case
    login: this is login case

6、xfail_strict

设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败

xfail_strict = True

 

posted @ 2020-12-09 16:42  eastonliu  阅读(5009)  评论(0编辑  收藏  举报