三、Pytest 配置详解

1. pytest配置文件 

pytest.ini 是pytest 的主配置文件,可以改变 pytest 的默认行为,pytest配置文件能够改变pytest框架代码的运行规则。比如修改pytest收集用例的规则,添加命令行参数等等!它是一个固定的文件pytest.ini文件,项目在运行时会首先按照配置文件中设置的参数选项来运行,其次再遵守pytest的默认规则

 

pytest里面的非test文件

  • pytest.ini pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py 测试用例的一些fixture配置
  • init.py 识别该文件夹为python的package包
  • tox.ini 与pytest.ini类似,用tox工具时候才有用
  • setup.cfg 也是ini格式文件,影响setup.py的行为
2. pytest.ini可配置的参数

通过命令pytest --help查看配置文件中可以添加的一些参数及选项,这些选项都可以添加到pytest的配置文件中

 

[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 dire

  console_output_style (string) console output: classic or with additional progr

  usefixtures (args)       list of default fixtures to be used with this project

  python_files (args)      glob-style file patterns for Python test module disco

  python_classes (args)    prefixes or glob names for Python test class discover

  python_functions (args)  prefixes or glob names for Python test function and m

  disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool) di

  xfail_strict (bool)      default for the strict parameter of xfail markers whe

  junit_suite_name (string) Test suite name for JUnit report

  junit_logging (string)   Write captured log messages to JUnit report: one of n

  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.

  filterwarnings (linelist) Each line specifies a pattern for warnings.filterwar

  log_print (bool)         default value for --no-print-logs

  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 "li

  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

  addopts (args)           extra command line options

  minversion (string)      minimally required pytest version

 

environment variables:

  PYTEST_ADDOPTS           extra command line options

  PYTEST_PLUGINS           comma-separated plugins to load during startup

  PYTEST_DISABLE_PLUGIN_AUTOLOAD set to disable plugin auto-loading

  PYTEST_DEBUG             set to enable debug tracing of pytest's internals

 

to see available markers type: pytest --markers

to see available fixtures type: pytest --fixtures

(shown according to specified file_or_dir or current dir if not specified; fixtures with leading '_' are only shown with the '-v' option

 

注意:

1,配置文件格式,配置多个参数时用空格隔开.

2,未解决编码问题时,配置文件尽量不要写入中文,运行时可能会报错

3,在命令行执行命令时,要在ini配置文件的路径执行,否则会读取不到配置文件

4,在哪个路径执行命令就读取那个路径里的配置文件,否则执行默认配置测试

 

pytest.ini文件格式

 

配置详解演示代码目录结构:

2.1 addopts 添加默认参数选项

addopts 参数可以更改默认命令行选顷,当我们在 cmd 输入指令去执行用例的时候,会用到很多参数,比如我想测试完生成报告,指令比较长

> pytest -rsxX -vqs --html=report.html --self-contained-html

每次输入这么多,不太好记住,可以加到 pytest.ini 中,执行时只要在命令行执行pytest,会自动执行配置在配置文件的参数

2.2 testpaths 添加用例路径

通过这样一项设置,我们可以把用例所在的目录添加到配置文件,这样我们在运行用例的时候,pytest会直接在配置文件配置的目录搜索用例,路径支持配置相对路径和绝对路径。

 

#pytest.ini

testpaths=./test_codemao C:\\Users\\codemao\\PycharmProjects\\test\\test_codemao2

2.3 norecursedirs  忽略搜索用例路径

我们可以把不需要执行的用例所在的目录添加到配置文件,这样我们在运行用例的时候,pytest会忽略在配置文件配置的norecursedirs下的路径,路径支持配置相对路径和绝对路径。

 

#pytest.ini

norecursedirs =./test_codemao C:\\Users\\codemao\\PycharmProjects\\test\\test_codemao2

2.4 markers 自定义标签

有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件。不配置在配置文件也可执行,执行pytest -m webtest命令执行相关用例会报警告。配置好之后,可以使用 pytest --markers 查看到可用的标记

 

#pytest.ini

markers =

 webtest: Run the webtest case

 hello: Run the hello case

 

#test_a.py

import pytest

 

def test_passing():

assert (1, 2, 3) == (1, 2, 3)

 

@pytest.mark.webtest

def test_passing2():

assert (1, 2, 3) == (1, 2, 3)

 

@pytest.mark.hello

@pytest.mark.xfail

def test_failing():

assert (1, 2, 3) == (3, 2, 1)

 

2.5 xfail_strict更改用例执行结果

设置 xfail_strict = true 可以让那些标记为@pytest.mark.xfail 但逻辑判断为真的测试用例被报告为Failed,逻辑判断为假的测试用例报告为XFailed,即预期失败

 

#test_a.py

import pytest

 

def test_passing():

assert (1, 2, 3) == (1, 2, 3)

 

@pytest.mark.webtest

@pytest.mark.xfail

def test_passing2():

assert (1, 2, 3) == (1, 2, 3)

 

@pytest.mark.hello

@pytest.mark.xfail

def test_failing():

assert (1, 2, 3) == (3, 2, 1)

 

执行结果

 

设置 xfail_strict = false 可以让那些标记为@pytest.mark.xfail 但逻辑判断为真的测试用例被报告为Xpass,即意外通过。逻辑判断为假的测试用例依然报告为XFailed,即预期失败

 

 
2.6 python_files 配置读取测试文件规则

pytest默认会搜索以test_开头或以_test结尾的文件,如果我们希望pytest能够同时搜索以codemao开头的文件,我们可以添加这样的选项,

 

#pytest.ini

python_files=codemao* test*

 

2.7 python_classes 配置读取测试类规则

pytest默认会搜索以Test开头的类。我们希望pytest能够搜索以Codemao开头的类名字,我们可以添加这样的选项

 

#pytest.ini

python_classes =Codemao*

 

2.8 python_functions 配置读取测试方法规则

pytest默认会搜索以test_开头的测试函数为测试用例。我们希望pytest能够搜索以codemao开头的用例名,我们需要添加这样的选项

 

#pytest.ini

python_functions =codemao* test*

 

2.9 cache_dir设置缓存文件夹路径

执行测试时会默认在执行的路径中生成pytest_cache文件夹,如果不想在这个地方出现这个文件夹,可以配置它的路径

 

#pytest.ini

cache_dir=C:\\Users\\codemao\\PycharmProjects\\.pytest_cache

 

2.10 confcutdir 配置读取contest.py文件路径

设置一个目录,在该目录中向上搜索contest.py文件。默认情况下,pytest将停止从项目的pytest.ini/ox.ini/setup.cfg(如果有的话)向上搜索contest.py文件,或者一直搜索文件系统根目录。 

(暂未看到什么影响)

 

2.11 console_output_style 运行测试时设置控制台输出样式

Classic:经典的PyTest输出。

progress:如经典的Pythest输出,但具有进度指示符。

count:类似进度,但显示完成测试次数而不是百分比的进度。默认情况是进度,但如果您更喜欢或新模式导致意外问题,您可以回退到经典: 

(暂未看到什么影响)

2.11 empty_parameter_set_mark 设置传参为空时的用例执行结果

如果parametrize收集空参数集,则允许在参数化跳过使用空参数集(默认值)xfail标记为空参数集的测试作为xfail(run=False)failcollect时拾取空参数集的操作。

xfail 标记空参数用例执行结果为xfail

skip 标记空参数用例执行结果为skip

fail_at_collect 会报错,用途不明

 

同时和@pytest.mark.xfail装饰器使用时,以该配置参数为准

 

#pytest.ini

empty_parameter_set_mark =skip

xfail_strict = true

 

#test_0.py

import pytest

@pytest.mark.parametrize("x", ())

@pytest.mark.xfail

def test_passing(x):

assert x == (1, 2, 3)

 

def test_passing2():

assert (1, 2, 3) == (1, 2, 3)

 

def test_failing():

assert (1, 2, 3) == (3, 2, 1)

 

posted @ 2020-06-17 15:34  hongliangsam  阅读(584)  评论(0编辑  收藏  举报