pytest.ini配置文件详解【pytest系列 12】

1、pytest中的配置文件的作用

  • pytest.ini pytest的主配置文件,可以改变pytest的默认行为,即pytest会读取该配置信息,然后按照指定的方式去运行
  • conftest.py 测试用例的一些fixture配置
  • __init.py__ 识别文件夹为package包

2、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
      pythonpath (paths):   Add paths to sys.path
      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.
    

3、常用配置项

首先,pytest.ini需要放置在项目根目录下

常用配置项一:markers (linelist): markers for test functions

  • 作用:测试用例中添加@pytest.mark.xxx,xxx为自定义的标签,如果不添加marks选项,就会报warnings
  • 写法:
    [pytest]
    markers =
        weibo: this is weibo page
        toutiao: this is toutiao page
        xinlang: this is xinlang page
    

常用配置二:xfail_strict (bool): default for the strict parameter of xfail markers when not given explicitly (default: False)

  • 作用:默认为False,如果设置xfail_strict=True可以让那些标记为@pytest.mark.xfail(),但实际通过的用例,显示为XPASS的用例,被报告为失败
  • 写法
    [pytest]
    xfail_strict = True
    

常用配置三:addopts (args): extra command line options

  • 作用:addopts表示额外的命令行参数。比如:想要通过生成测试报告、失败重跑两次、通过分布式去跑用例,如果使用命令行,每次都要输入如下命令:
    pytest -v --html-report=report.html --reruns=2 -n=auto
    
  • pytest.ini如下配置后,只需执行命令pytest即可完成同样的效果
    [pytest]
    addopts = -v --html=report.html --reruns=2 -n=auto
    

常用配置四:log_cli (bool): enable log display during test run (also known as "live logging").

  • 作用:控制日志是否实时输出(具体显示哪个module下的哪个用例在执行)
  • 配置为log_cli=False的日志输出
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnPytestIni, configfile: pytest.ini
    plugins: metadata-2.0.1, rerunfailures-10.2, html-3.1.1, repeat-0.9.1
    collected 1 item                                                                                                                                                           
    
    test_01.py .                                                                                                                                                         [100%]
    
    ============================================================================ 1 passed in 0.01s =============================================================================
    
  • 配置为log_cli=True的日志输出
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnPytestIni, configfile: pytest.ini
    plugins: metadata-2.0.1, rerunfailures-10.2, html-3.1.1, repeat-0.9.1
    collected 1 item                                                                                                                                                           
    
    test_01.py::test_01 PASSED                                                                                                                                           [100%]
    
    ============================================================================ 1 passed in 0.01s =============================================================================
    

常用配置五:更改用例收集规则

  • pytest默认的测试用例收集规则:
    • 文件名为test_*.py*_test.py
    • test_开头的函数
    • Test开头的类,不能包含__init__方法
  • 说明
    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
    
  • 一般建议在原有的规则上添加新的收集规则:
    [pytest]
    python_files = test_* *_test
    python_classes = Test*
    python_functions = test_* newtest_*
    

参考文章:https://www.cnblogs.com/poloyy/p/12702294.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值