所在位置:首页 → 软件测试 → 测试框架 → Pytest高级运用之pytest.ini配置文件详解

Pytest高级运用之pytest.ini配置文件详解

发布: 更新时间:2022-09-07 16:46:18

作用:pytest.ini配置文件可以改变pytest的运行方式,读取配置信息,按指定的方式去运行。

pytest里有些文件是非test文件

  • pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture配置
  • _init_.py:识别该文件夹为python的package包

一、pytest.ini存放位置

  一般放在项目根目录下,不要乱放,也不要乱起其他名字

二、常用的配置项(重要)

[pytest]
addopts = "-vs"   #加不加引号都可以
testpaths = "./test_case"
python_files = "api*.py"
python_classes = "Aaa"
python_functions = "cc"
markers =
    smoke:冒烟测试用例
    usermanage:用户管理模块测试用例

(1)addopts

作用:主要用来配置运行用例时,需要的参数

示例pytest -vs。我们可以把执行参数写到pytest.ini里面这样写:addopts = -vs

(2)testpaths

作用:配置默认读取执行的项目路径

示例:pytest ./test_case。我们可以把执行参数写到pytest.ini里面这样写:testpaths = ./test_case

(3)python_files

作用:模块的命名规则,pytest默认查找的模块是以test开头或结尾的。可以更改此规则。

示例:python_files = api*.py。默认搜索以api开头的模块

(4)python_classes

作用:类的命名规则,pytest默认查找的类是以Test开头的。可以更改此规则。

示例:python_class = Aaa。默认搜索以Aaa开头的类

(5)python_functions

作用:方法的命名规则,pytest默认查找的方法是以test开头的。可以更改此规则。

示例:python_functions = cc。默认搜索以cc开头的方法

(6)markers

作用:用例分组管理,贴标签。

[pytest]
markers =
    P0: Run the P0 case
    P1: Run the P1 case
    P2: Run the P2 case

在pytest.ini里面配置markers。然后测试类或测试用例前使用装饰器@pytest.mark.P0,执行时使用pytest -m "P0" ,即可执行特定标签的用例啦

查看标记:pytest --markers

(7)norecursedirs

作用:搜索时忽略目录。norecurse的默认设置是  .*  build  dist CVS -darcs {arch} 和 *.egg。因为有  .*,所以将虚拟环境命名为.venv是一个好注意,所有以.(点)开头的目录都不会被访问。如果不是以.(点)开头,那么需要把它加入norecursedirs里。

示例:比如我想忽略test_001目录,norecursedirs = .* venv test_001 *.egg dist build

(8)xfail_strict

作用:设置xfail_strict=True将会使那些被标记为@pytest.mark.xfail但实际通过的测试用例也被报告为失败

格式:True、False(默认),1、0

import pytest

@pytest.mark.xfail()
def test_answer():
    assert 5 == 5

@pytest.mark.xfail()
def test_answer1():
    assert 5 != 5

pytest.ini中设置:xfail_strict = true

本来是Pass的用例,也被报告成FAILED

(9)minversion

作用:指定运行测试用例的pytest的最低版本。

示例:minversion = 4.0。当pytest版本小于此时4.0时,会报错

(10)cache_dir

作用:设置cache目录

(11)pythonpath

作用:添加路径到sys.path

(12)required_plugins

作用:设置哪些插件在运行pytest前必须安装好。

三、其它配置项

empty_parameter_set_mark (string)default marker for empty parametersets
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
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").
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
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.
标签:, , ,
文章排行