专栏首页橙子探索测试Pytest配置文件pytest.ini

Pytest配置文件pytest.ini

pytest.ini文件是pytest的主配置文件,可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。pytest.ini的位置:一般放在项目工程的根目录(即当前项目的顶级文件夹下)

cmd下使用 pytest -h 命令查看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)
  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|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
  faulthandler_timeout (string):
                        Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Not
                        available on Windows.
  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

addopts配置

addopts参数可以更改默认命令行选项,可以将一些命令添加到pytest.ini里就不需要每次命令行执行时都带上了,默认就会以pytest.ini里配置去运行,多个命令行参数用空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数

[pytest]
addopts = -v -reruns 1 --html=../report/report.html

当pytest.ini未配置addopts = -v时,py文件里执行pytest.main(["test_001_rights.py"])或cmd下执行pytest,未带-v,执行结果不会有详细信息,需带上-v才会有详细信息,如果我们想执行时不带-v还能有详细信息,这时就需要在pytest.ini里配置addopts = -v

# py文件里执行pytest.main(["test_001_rights.py"])

if __name__ == "__main__":
    pytest.main(["test_001_rights.py"])
    
"C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/test_case/srzp/test_001_rights.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 2 items

test_001_rights.py ..                                                    [100%]

============================== 2 passed in 3.21s ==============================

Process finished with exit code 0


# cmd下执行pytest
C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>pytest
=============================================== test session starts ================================================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 2 items                                                                                                   

test_001_rights.py ..                                                                                         [100%]

================================================ 2 passed in 2.40s =================================================

C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>

当pytest.ini已配置addopts = -v时,py文件里执行pytest.main(["test_001_rights.py"])或cmd下执行pytest,未带-v,就会以配置的方式去执行,结果有详细的信息

# py文件里执行pytest.main(["test_001_rights.py"])

if __name__ == "__main__":
    pytest.main(["test_001_rights.py"])
    
"C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/test_case/srzp/test_001_rights.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Packages': {'py': '1.8.0', 'pluggy': '0.12.0', 'pytest': '5.1.2'}, 'Platform': 'Windows-10-10.0.18362-SP0', 'Plugins': {'rerunfailures': '8.0', 'html': '1.22.0', 'metadata': '1.8.0', 'allure-pytest': '2.8.5'}, 'Python': '3.5.2'}
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collecting ... collected 2 items

test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\1.0\business\class\list?page=1&page_size=10-None0] PASSED [ 50%]
test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\1.0\business\class\list?page=1&page_size=10-None1] PASSED [100%]

============================== 2 passed in 2.52s ==============================

Process finished with exit code 0

# cmd下执行pytest
C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>pytest
=============================================== test session starts ================================================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- c:\program files\python35\python.exe
cachedir: .pytest_cache
metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Packages':
 {'py': '1.8.0', 'pytest': '5.1.2', 'pluggy': '0.12.0'}, 'Plugins': {'html': '1.22.0', 'allure-pytest': '2.8.5', 'rer
unfailures': '8.0', 'metadata': '1.8.0'}, 'Python': '3.5.2'}
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 2 items                                                                                                   

test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\1.0\business\class\list?page=1&pa
ge_size=10-None0] PASSED [ 50%]
test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\1.0\business\class\list?page=1&pa
ge_size=10-None1] PASSED [100%]

================================================ 2 passed in 3.48s =================================================

testpaths配置

pytest默认是搜索执行当前目录下的所有用例,当pytest.ini配置了testpaths = test_case/lxk或testpaths = test_case/lxk/test_001_case.py就会只执行当前配置的文件夹下或文件里的用例,这样我们就可以灵活的控制运行需要测试的用例了,可配置多个,空格隔开

项目目录:

当pytest.ini未配置testpaths时,会按pytest默认搜索执行方式,run_all_case.py里执行pytest.main() lxk和srzp文件夹下的用例都执行了

"C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.5.2', 'Packages': {'pytest': '5.1.2', 'pluggy': '0.12.0', 'py': '1.8.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Platform': 'Windows-10-10.0.18362-SP0', 'Plugins': {'allure-pytest': '2.8.5', 'metadata': '1.8.0', 'rerunfailures': '8.0', 'html': '1.22.0'}}
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collecting ... collected 8 items

test_case/lxk/test_001_case.py::Test::test_001 PASSED                    [ 12%]
test_case/lxk/test_001_case.py::Test::test_002 PASSED                    [ 25%]
test_case/lxk/test_002_case.py::Test::test_001 PASSED                    [ 37%]
test_case/lxk/test_002_case.py::Test::test_002 PASSED                    [ 50%]
test_case/srzp/test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-/1.0/business/class/list?page=1&page_size=10-None0] PASSED [ 62%]
test_case/srzp/test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-/1.0/business/class/list?page=1&page_size=10-None1] PASSED [ 75%]
test_case/srzp/test_002_case.py::Test::test_001_login PASSED             [ 87%]
test_case/srzp/test_002_case.py::Test::test_002_order PASSED             [100%]

============================== warnings summary ===============================
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.py:324
  C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 8 passed, 1 warnings in 1.80s ========================

Process finished with exit code 0

当pytest.ini已配置testpaths = test_case/lxk,run_all_case.py里执行pytest.main() 只会运行lxk文件夹下的用例

"C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Plugins': {'html': '1.22.0', 'allure-pytest': '2.8.5', 'metadata': '1.8.0', 'rerunfailures': '8.0'}, 'Packages': {'pytest': '5.1.2', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Platform': 'Windows-10-10.0.18362-SP0'}
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini, testpaths: test_case/lxk
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collecting ... collected 4 items

test_case/lxk/test_001_case.py::Test::test_001 PASSED                    [ 25%]
test_case/lxk/test_001_case.py::Test::test_002 PASSED                    [ 50%]
test_case/lxk/test_002_case.py::Test::test_001 PASSED                    [ 75%]
test_case/lxk/test_002_case.py::Test::test_002 PASSED                    [100%]

============================== warnings summary ===============================
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.py:324
  C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 4 passed, 1 warnings in 0.07s ========================

Process finished with exit code 0

norecursedirs配置

pytest.ini配置norecursedirs= lxk test.py 不搜索执行对应文件夹下或文件下的用例,和testpaths配置完全相反的效果,可配置多个,空格隔开

python_files (args)配置

pytest默认是匹配test_*.py、 *_test.py文件,如果配置python_files = smoke.py 匹配 python 用例文件, 如smoke_*.py、 *_smoke.py 可配置多个,空格隔开

目录:

run_all_case运行pytest.main()可以看到只运行了smoke.py文件里的用例

"C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Packages': {'pytest': '5.1.2', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Python': '3.5.2', 'Plugins': {'metadata': '1.8.0', 'rerunfailures': '8.0', 'allure-pytest': '2.8.5', 'html': '1.22.0'}}
rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collecting ... collected 2 items

test_case/lxk/smoke.py::Test::test_001 PASSED                            [ 50%]
test_case/lxk/smoke.py::Test::test_002 PASSED                            [100%]

============================== warnings summary ===============================
C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.py:324
  C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 2 passed, 1 warnings in 0.17s ========================

Process finished with exit code 0

python_classes (args)配置

配置python_classes =Test* 匹配class 类名称 如Test*,可配置多个,空格隔开,和python_files (args)方法配置类似

python_functions (args)配置

配置python_functions = test_* 匹配函数和class里面方法 如test_*可配置多个,空格隔开,和python_files (args)方法配置类似

本文分享自微信公众号 - 橙子探索测试(PerfectTest_),作者:王荔探索测试

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-09-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Pytest跳过执行之@pytest.mark.skip()详解

    在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释...

    橙子探索测试
  • Pytest自定义标记mark及指定文件/类/方法/用例执行

    pytest.main(['-s','test01.py','-m=test'])

    橙子探索测试
  • Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序

    pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的,文件里的用例是从上往下按顺序执行的.

    橙子探索测试
  • pytest文档22-pytest分布式执行(pytest-xdist)

    平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟。如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试...

    上海-悠悠
  • 《带你装B,带你飞》pytest成神之路2- 执行用例规则和pycharm运行的三种姿态

      今天北京下的雪好大好美啊!!!哎呀,忘记拍照片了,自己想象一下吧。言归真传,今天还是开始pytest的学习和修炼,上一篇写完后群里反响各式各样的,几家欢乐几...

    北京-宏哥
  • pytest 测试框架学习(1):初识

    3.3. 在输入框中输入 pytest 后,点击下方 Install Package,安装成功会弹出 install successfully 的提示信息。...

    Mokwing
  • 初步了解Pytest测试框架

    最近加班再加上自己的懒惰,很久没有更新了,想想你停留在原地的时候,其他人正在努力,那你就会越来越落后,每天进步一点点,加油!!!关注公众号【‘软件测试小助手’】...

    小雯子打豆豆
  • Pytest系列(1) - 快速入门和基础讲解

    https://www.cnblogs.com/poloyy/category/1690628.html

    小菠萝测试笔记
  • Pytest自定义标记mark及指定文件/类/方法/用例执行

    pytest.main(['-s','test01.py','-m=test'])

    橙子探索测试
  • Pytest跳过执行之@pytest.mark.skip()详解

    在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释...

    橙子探索测试

扫码关注云+社区

领取腾讯云代金券