1. 程式人生 > 其它 >Pytest 外掛介紹和開發

Pytest 外掛介紹和開發

外掛分類

外掛分三種:

1.外部外掛(三方開發的)

安裝方式:pip install

哪裡可以找到三方外掛: https://pypi.org/ 

常用外掛:

 pytest-ordering

作用:指定執行順序

使用:(fixture用法類似)@pytest.mark.run(order=1) 

* 儘量不要指定用例順序,避免發現不到問題

pytest-xdist

作用:可以讓用例並行與分散式執行

使用:命令列輸入 pytest -n CPU核數 xxx.py或者pytest -n auto

2.本地外掛

本地外掛就是fixture執行

pytest自動模組發現機制執行fixture(conftest.py存放的)

3.內建外掛

內建外掛是指勾子函式Hook,pytest執行其實會執行很多函式。具體執行順序可看:https://ceshiren.com/t/topic/8807

pytest hook 介紹

  • 是個函式,在系統訊息觸時被系統呼叫
  • 自動觸發機制
  • Hook 函式的名稱是確定的
  • pytest有非常多的勾子函式
  • 使用時直接編寫函式體

*pytest執行順序

以下就是為Hook函式,存在 ...\Python38\Lib\site-packages\_pytest\hookspec.py

如果我們需要使用,只需要將這個函式在conftest.py裡面重新改寫即可

root
└── pytest_cmdline_main
├── pytest_plugin_registered
├── pytest_configure
│ └── pytest_plugin_registered
├── pytest_sessionstart
│ ├── pytest_plugin_registered
│ └── pytest_report_header
├── pytest_collection
│ ├── pytest_collectstart
│ ├── pytest_make_collect_report
│ │ ├── pytest_collect_file
│ │ │ └── pytest_pycollect_makemodule
│ │ └── pytest_pycollect_makeitem
│ │ └── pytest_generate_tests
│ │ └── pytest_make_parametrize_id
│ ├── pytest_collectreport
│ ├── pytest_itemcollected
│ ├── pytest_collection_modifyitems
│ └── pytest_collection_finish
│ └── pytest_report_collectionfinish
├── pytest_runtestloop
│ └── pytest_runtest_protocol
│ ├── pytest_runtest_logstart
│ ├── pytest_runtest_setup
│ │ └── pytest_fixture_setup
│ ├── pytest_runtest_makereport
│ ├── pytest_runtest_logreport
│ │ └── pytest_report_teststatus
│ ├── pytest_runtest_call
│ │ └── pytest_pyfunc_call
│ ├── pytest_runtest_teardown
│ │ └── pytest_fixture_post_finalizer
│ └── pytest_runtest_logfinish
├── pytest_sessionfinish
│ └── pytest_terminal_summary
└── pytest_unconfigure