欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Pytest框架学习14--pytest.ini

程序员文章站 2024-02-27 14:41:09
...

pytest.ini文件是pytest的主配置文件,可以改变pytest的默认行为。

使用pytest --help指令可以查看pytest.ini的设置选项

[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.
  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)
  junit_suite_name (string):
                        Test suite name for JUnit report
  junit_logging (string):
                        Write captured log messages to JUnit report: one of
                        no|system-out|system-err
  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.
  filterwarnings (linelist):
                        Each line specifies a pattern for
                        warnings.filterwarnings. Processed after -W and
                        --pythonwarnings.
  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 "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
  addopts (args):       extra command line options
  minversion (string):  minimally required pytest version

常用配置项介绍:

1.marks标记,测试用例中添加了@pytest.mark.webtest装饰器的,运行后仅会运行带标记的用例

[pytest]
markers=webtest:1111111111111 (标签注释)
        hello:22222222222222
        


import pytest


def test_3():
    print(33333333)


@pytest.mark.webtest
def test_4():
    print(444444444444)



(venv) E:\PycharmProject\TestLibrary1\Pytttest\1111>pytest -m=webtest test_22.py -s -v
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.7.0, pytest-5.1.3, py-1.8.0, pluggy-0.13.0 -- e:\pycharmproject\testlibrary1\venv\scripts\python3.exe
cachedir: .pytest_cache
rootdir: E:\PycharmProject\TestLibrary1\Pytttest\1111, inifile: pytest.ini
collected 2 items / 1 deselected / 1 selected                                                                                                                                                                                             

test_22.py::test_4 444444444444
PASSED

==================================================================================================== 1 passed, 1 deselected in 0.01s ====================

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

import pytest


@pytest.mark.xfail()
def test_3():
    print(33333333)


@pytest.mark.xfail()
def test_4():
    print(444444444444)

没有加xfail_strict = true,运行结果如下:都是xpassed
E:\PycharmProject\TestLibrary1\venv\Scripts\python3.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pycharm\_jb_pytest_runner.py" --path E:/PycharmProject/TestLibrary1/Pytttest/1111/test_22.py
Launching pytest with arguments E:/PycharmProject/TestLibrary1/Pytttest/1111/test_22.py in E:\PycharmProject\TestLibrary1\Pytttest\1111
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.1.3, py-1.8.0, pluggy-0.13.0
rootdir: E:\PycharmProject\TestLibrary1\Pytttest\1111, inifile: pytest.inicollected 2 items

test_22.py X33333333
X444444444444
                                                            [100%]

============================= 2 xpassed in 0.01s ==============================

Process finished with exit code 0

pytest.ini添加xfail_strict = true后:可以看到结果变成failed了
E:\PycharmProject\TestLibrary1\venv\Scripts\python3.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pycharm\_jb_pytest_runner.py" --path E:/PycharmProject/TestLibrary1/Pytttest/1111/test_22.py
Launching pytest with arguments E:/PycharmProject/TestLibrary1/Pytttest/1111/test_22.py in E:\PycharmProject\TestLibrary1\Pytttest\1111
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.1.3, py-1.8.0, pluggy-0.13.0
rootdir: E:\PycharmProject\TestLibrary1\Pytttest\1111, inifile: pytest.inicollected 2 items

test_22.py F33333333

test_22.py:5 (test_3)
[XPASS(strict)] 
F444444444444

test_22.py:10 (test_4)
[XPASS(strict)] 
                                                            [100%]

================================== FAILURES ===================================
___________________________________ test_3 ____________________________________
[XPASS(strict)] 
---------------------------- Captured stdout call -----------------------------
33333333
___________________________________ test_4 ____________________________________
[XPASS(strict)] 
---------------------------- Captured stdout call -----------------------------
444444444444
============================== 2 failed in 0.01s ==============================

Process finished with exit code 0

Assertion failed

Assertion failed

3.addopts参数可以更改默认命令行选项,可以将一些命令添加后就不需要每次命令行执行时都添加

(venv) E:\PycharmProject\TestLibrary1\Pytttest\1111>pytest
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.7.0, pytest-5.1.3, py-1.8.0, pluggy-0.13.0
rootdir: E:\PycharmProject\TestLibrary1\Pytttest\1111, inifile: pytest.ini
collected 8 items                                                                                                                                                                                                                         

test_11.py ..                                                                                                                                                                                                                       [ 25%]
test_22.py F.                                                                                                                                                                                                                       [ 50%]
test_autotest.py ..                                                                                                                                                                                                                 [ 75%]
test_login.py .x                                                                                                                                                                                                                    [100%]

================================================================================================================ FAILURES ================================================================================================================
_________________________________________________________________________________________________________________ test_3 _________________________________________________________________________________________________________________
[XPASS(strict)]
---------------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------------
33333333
================================================================================================= 1 failed, 6 passed, 1 xfailed in 0.06s =================================================================================================



添加 addopts=-v -s  后运行代码
(venv) E:\PycharmProject\TestLibrary1\Pytttest\1111>pytest
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.7.0, pytest-5.1.3, py-1.8.0, pluggy-0.13.0 -- e:\pycharmproject\testlibrary1\venv\scripts\python3.exe
cachedir: .pytest_cache
rootdir: E:\PycharmProject\TestLibrary1\Pytttest\1111, inifile: pytest.ini
collected 8 items                                                                                                                                                                                                                         

test_11.py::test1 1111111111
PASSED
test_11.py::test2 2222222222
PASSED
test_22.py::test_3 33333333
FAILED
test_22.py::test_4 444444444444
PASSED
test_autotest.py::test_ll 登录中
test_11
PASSED
test_autotest.py::test_l2l 登录中
test_22
PASSED
test_login.py::testlogin 登录失败
PASSED
test_login.py::testchange_name 登录失败
XFAIL

================================================================================================================ FAILURES ================================================================================================================
_________________________________________________________________________________________________________________ test_3 _________________________________________________________________________________________________________________
[XPASS(strict)]
================================================================================================= 1 failed, 6 passed, 1 xfailed in 0.06s =================================================================================================