小言_互联网的博客

Pytest配置文件pytest.ini

439人阅读  评论(0)

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

cmd下使用 pytest -h 命令查看pytest.ini的设置选项


   
  1. [pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
  2. markers (linelist): markers for test functions
  3. empty_parameter_set_mark (string):
  4. default marker for empty parametersets
  5. norecursedirs (args): directory patterns to avoid for recursion
  6. testpaths (args): directories to search for tests when no files or directories are given in the command line.
  7. usefixtures (args): list of default fixtures to be used with this project
  8. python_files (args): glob- style file patterns for Python test module discovery
  9. python_classes (args):
  10. prefixes or glob names for Python test class discovery
  11. python_functions (args):
  12. prefixes or glob names for Python test function and method discovery
  13. disable_test_id_escaping_and_forfeit_all_rights_to_community_support ( bool):
  14. disable string escape non- ascii characters, might cause unwanted side effects( use at your own
  15. risk)
  16. console_output_style ( string):
  17. console output: "classic", or with additional progress information ( "progress" (percentage) |
  18. "count").
  19. xfail_strict ( bool): default for the strict parameter of xfail markers when not given explicitly ( default: False)
  20. enable_assertion_pass_hook ( bool):
  21. Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache
  22. files.
  23. junit_suite_name ( string):
  24. Test suite name for JUnit report
  25. junit_logging ( string):
  26. Write captured log messages to JUnit report: one of no| system- out| system-err
  27. junit_log_passing_tests ( bool):
  28. Capture log information for passing tests to JUnit report:
  29. junit_duration_report ( string):
  30. Duration time to report: one of total| call
  31. junit_family ( string):
  32. Emit XML for schema: one of legacy|xunit1|xunit2
  33. doctest_optionflags (args):
  34. option flags for doctests
  35. doctest_encoding ( string):
  36. encoding used for doctest files
  37. cache_dir ( string): cache directory path.
  38. filterwarnings (linelist):
  39. Each line specifies a pattern for warnings.filterwarnings. Processed after -W and
  40. --pythonwarnings.
  41. log_print ( bool): default value for --no-print-logs
  42. log_level ( string): default value for --log-level
  43. log_format ( string): default value for --log-format
  44. log_date_format ( string):
  45. default value for --log-date-format
  46. log_cli ( bool): enable log display during test run (also known as "live logging").
  47. log_cli_level ( string):
  48. default value for --log-cli-level
  49. log_cli_format ( string):
  50. default value for --log-cli-format
  51. log_cli_date_format ( string):
  52. default value for --log-cli-date-format
  53. log_file ( string): default value for --log-file
  54. log_file_level ( string):
  55. default value for --log-file-level
  56. log_file_format ( string):
  57. default value for --log-file-format
  58. log_file_date_format ( string):
  59. default value for --log-file-date-format
  60. faulthandler_timeout ( string):
  61. Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Not
  62. available on Windows.
  63. addopts (args): extra command line options
  64. minversion ( string): minimally required pytest version
  65. environment variables:
  66. PYTEST_ADDOPTS extra command line options
  67. PYTEST_PLUGINS comma-separated plugins to load during startup
  68. PYTEST_DISABLE_PLUGIN_AUTOLOAD set to disable plugin auto-loading
  69. PYTEST_DEBUG set to enable debug tracing of pytest 's internals
  70. to see available markers type: pytest --markers
  71. to see available fixtures type: pytest --fixtures
  72. (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里配置去运行,多个命令行参数用空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数


   
  1. [pytest]
  2. 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


   
  1. # py文件里执行pytest.main([ "test_001_rights.py"])
  2. if __name__ == "__main__":
  3. pytest.main([ "test_001_rights.py"])
  4. "C:\Program Files\Python35\python.exe" C: /Users/wangli/Desktop/InterfaceAutoPytest/test_case/srzp/test_001_rights.py
  5. ============================= test session starts =============================
  6. platform win32 -- Python 3.5 .2, pytest -5.1 .2, py -1.8 .0, pluggy -0.12 .0
  7. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
  8. plugins: allure-pytest -2.8 .5, html -1.22 .0, metadata -1.8 .0, rerunfailures -8.0
  9. collected 2 items
  10. test_001_rights.py .. [ 100%]
  11. ============================== 2 passed in 3.21s ==============================
  12. Process finished with exit code 0
  13. # cmd下执行pytest
  14. C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>pytest
  15. =============================================== test session starts ================================================
  16. platform win32 -- Python 3.5 .2, pytest -5.1 .2, py -1.8 .0, pluggy -0.12 .0
  17. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
  18. plugins: allure-pytest -2.8 .5, html -1.22 .0, metadata -1.8 .0, rerunfailures -8.0
  19. collected 2 items
  20. test_001_rights.py .. [ 100%]
  21. ================================================ 2 passed in 2.40s =================================================
  22. C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>

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


   
  1. # py文件里执行pytest.main([ "test_001_rights.py"])
  2. if __name__ == "__main__":
  3. pytest.main([ "test_001_rights.py"])
  4. "C:\Program Files\Python35\python.exe" C: /Users/wangli/Desktop/InterfaceAutoPytest/test_case/srzp/test_001_rights.py
  5. ============================= test session starts =============================
  6. platform win32 -- Python 3.5 .2, pytest -5.1 .2, py -1.8 .0, pluggy -0.12 .0 -- C:\Program Files\Python35\python.exe
  7. cachedir: .pytest_cache
  8. 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'}
  9. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
  10. plugins: allure-pytest -2.8 .5, html -1.22 .0, metadata -1.8 .0, rerunfailures -8.0
  11. collecting ... collected 2 items
  12. 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%]
  13. 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%]
  14. ============================== 2 passed in 2.52s ==============================
  15. Process finished with exit code 0
  16. # cmd下执行pytest
  17. C:\Users\wangli\Desktop\InterfaceAutoPytest\test_case\srzp>pytest
  18. =============================================== test session starts ================================================
  19. platform win32 -- Python 3.5 .2, pytest -5.1 .2, py -1.8 .0, pluggy -0.12 .0 -- c:\program files\python35\python.exe
  20. cachedir: .pytest_cache
  21. metadata: { 'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Packages':
  22. { 'py': '1.8.0', 'pytest': '5.1.2', 'pluggy': '0.12.0'}, 'Plugins': { 'html': '1.22.0', 'allure-pytest': '2.8.5', 'rer
  23. unfailures': '8.0', 'metadata': '1.8.0'}, 'Python': '3.5.2'}
  24. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
  25. plugins: allure-pytest -2.8 .5, html -1.22 .0, metadata -1.8 .0, rerunfailures -8.0
  26. collected 2 items
  27. test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\ 1.0\business\ class\list?page= 1&pa
  28. ge_size= 10-None0] PASSED [ 50%]
  29. test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u6210\u529f-get-\ 1.0\business\ class\list?page= 1&pa
  30. ge_size= 10-None1] PASSED [ 100%]
  31. ================================================ 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文件夹下的用例都执行了


   
  1. "C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.5. 2, pytest- 5.1. 2, py- 1.8. 0, pluggy- 0. 12.0 -- C:\Program Files\Python35\python.exe
  4. cachedir: .pytest_cache
  5. 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'}}
  6. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
  7. plugins: allure-pytest- 2.8. 5, html- 1.22. 0, metadata- 1.8. 0, rerunfailures- 8.0
  8. collecting ... collected 8 items
  9. test_case/lxk/test_001_case.py::Test::test_001 PASSED [ 12%]
  10. test_case/lxk/test_001_case.py::Test::test_002 PASSED [ 25%]
  11. test_case/lxk/test_002_case.py::Test::test_001 PASSED [ 37%]
  12. test_case/lxk/test_002_case.py::Test::test_002 PASSED [ 50%]
  13. test_case/srzp/test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u621 0\u529f-get- /1.0/business /class/list?page= 1&page_size= 10-None 0] PASSED [ 62%]
  14. test_case/srzp/test_001_rights.py::Test::test_apply_task[\u83b7\u53d6\u5217\u8868\u621 0\u529f-get- /1.0/business /class/list?page= 1&page_size= 10-None1] PASSED [ 75%]
  15. test_case/srzp/test_002_case.py::Test::test_001_login PASSED [ 87%]
  16. test_case/srzp/test_002_case.py::Test::test_002_order PASSED [ 100%]
  17. ============================== warnings summary ===============================
  18. C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures. py: 324
  19. 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
  20. PytestUnknownMarkWarning,
  21. -- Docs: https:/ /docs.pytest.org/en /latest/warnings.html
  22. ======================== 8 passed, 1 warnings in 1.80s ========================
  23. Process finished with exit code 0

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


   
  1. "C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.5. 2, pytest- 5.1. 2, py- 1.8. 0, pluggy- 0. 12.0 -- C:\Program Files\Python35\python.exe
  4. cachedir: .pytest_cache
  5. 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'}
  6. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini, testpaths: test_case/lxk
  7. plugins: allure-pytest- 2.8. 5, html- 1.22. 0, metadata- 1.8. 0, rerunfailures- 8.0
  8. collecting ... collected 4 items
  9. test_case/lxk/test_001_case.py::Test::test_001 PASSED [ 25%]
  10. test_case/lxk/test_001_case.py::Test::test_002 PASSED [ 50%]
  11. test_case/lxk/test_002_case.py::Test::test_001 PASSED [ 75%]
  12. test_case/lxk/test_002_case.py::Test::test_002 PASSED [ 100%]
  13. ============================== warnings summary ===============================
  14. C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures. py: 324
  15. 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
  16. PytestUnknownMarkWarning,
  17. -- Docs: https:/ /docs.pytest.org/en /latest/warnings.html
  18. ======================== 4 passed, 1 warnings in 0. 07s ========================
  19. 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文件里的用例


   
  1. "C:\Program Files\Python35\python.exe" C:/Users/wangli/Desktop/InterfaceAutoPytest/run_all_case.py
  2. ============================= test session starts =============================
  3. platform win32 -- Python 3.5. 2, pytest- 5.1. 2, py- 1.8. 0, pluggy- 0. 12.0 -- C:\Program Files\Python35\python.exe
  4. cachedir: .pytest_cache
  5. 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'}}
  6. rootdir: C:\Users\wangli\Desktop\InterfaceAutoPytest, inifile: pytest.ini
  7. plugins: allure-pytest- 2.8. 5, html- 1.22. 0, metadata- 1.8. 0, rerunfailures- 8.0
  8. collecting ... collected 2 items
  9. test_case/lxk/smoke.py::Test::test_001 PASSED [ 50%]
  10. test_case/lxk/smoke.py::Test::test_002 PASSED [ 100%]
  11. ============================== warnings summary ===============================
  12. C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures. py: 324
  13. 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
  14. PytestUnknownMarkWarning,
  15. -- Docs: https:/ /docs.pytest.org/en /latest/warnings.html
  16. ======================== 2 passed, 1 warnings in 0. 17s ========================
  17. 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)方法配置类似


转载:https://blog.csdn.net/qq_36502272/article/details/108557806
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场