之前用到的一些pytest插件丑罪,在這記錄一下纳本。
pytest-sugar
- 作用:改變pytest執(zhí)行的默認(rèn)外觀您机。
- 安裝:
pip install pytest-sugar
$ pytest -v test_demo.py
Test session starts (platform: darwin, Python 3.7.3, pytest 4.5.0, pytest-sugar 0.9.2)
cachedir: .pytest_cache
rootdir: /Users/libo/Learn
plugins: sugar-0.9.2
collecting ...
test_demo.py::test_one ? 33% ███▍
test_demo.py::test_two ? 67% ██████▋
test_demo.py::test_three ? 100% ██████████
Results (0.02s):
3 passed
pytest-ordering
- 作用:改變pytest默認(rèn)的執(zhí)行順序例嘱;
- 安裝:
pip install pytest-ordering
- 用法:通過給用例添加
@pytest.mark.run(order=x)
裝飾器蒋得,例如:
import pytest
@pytest.mark.run(order=2)
def test_one():
assert True
@pytest.mark.run(order=1)
def test_two():
assert True
本來按順序執(zhí)行應(yīng)該是先執(zhí)行test_one
然后再執(zhí)行test_two
卵沉,但是加了@pytest.mark.run(order=1)
之后用例的執(zhí)行順序變了:
$ pytest -v test_order.py
Test session starts (platform: darwin, Python 3.7.3, pytest 4.3.1, pytest-sugar 0.9.2)
cachedir: .pytest_cache
rootdir: /Users/libo/Learn, inifile:
plugins: sugar-0.9.2, ordering-0.6
collecting ...
test_order.py::test_two ? 50% █████
test_order.py::test_one ? 100% ██████████
Results (0.02s):
2 passed
pytest-picked
- 作用:依賴git颠锉,僅僅執(zhí)行你已經(jīng)修改但是還沒提交的測試用例;
- 安裝:
pip install pytest-picked
- 用法:
pytest --picked
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
api.py
tests/api/
tests/test_board.py
nothing added to commit but untracked files present (use "git add" to track)
可以看到?jīng)]有執(zhí)行的為api.py
, tests/api/
, tests/test_board.py
.
此時執(zhí)行pytest --picked
:
$ pytest --picked
============================= test session starts =============================
platform darwin -- Python 3.6.4, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /Users/ana.gomes/personal-workspace/grandma, inifile:
plugins: picked-0.1.0, mock-1.10.0, flask-0.10.0, deadfixtures-2.0.1
collecting 34 items
Changed test files... 1. ['tests/test_board.py']
Changed test folders... 1. ['tests/api/']
collected 34 items
tests/test_board.py . [ 50%]
tests/api/test_new.py . [100%]
=========================== 2 passed in 0.07 seconds ===========================
pytest-rerunfailures
- 作用:重新運行失敗的測試用例史汗;
- 安裝:
pip install pytest-rerunfailures
- 用法:
pytest --reruns 5
pytest --reruns 5 --reruns-delay 1
--reruns-delay指定重新運行的延遲 - 指定單個用例:
@pytest.mark.flaky(reruns=5)
def test_example():
import random
assert random.choice([True, False])
pytest-xdist
- 作用:并發(fā)執(zhí)行測試用例琼掠;
- 安裝:
pip install pytest-xdist
- 用法:
pytest -n NUM
NUM表示執(zhí)行的CPU數(shù)量,也可以使用auto
來自動識別系統(tǒng)的cpu數(shù)量并執(zhí)行停撞;
如果一個用例失敗導(dǎo)致python解釋器crash掉瓷蛙,pytest-xdist
會自動重啟該解釋器所在的worker并且正常報告錯誤,你可以使用--max-worker-restart
選項來限制可以重新啟動的worker戈毒,或者使用--max-worker-restart=0
完全禁用重新啟動艰猬。
pytest-parallel
- 作用:使用多處理(并行)和多線程(并發(fā))快速運行測試;
- 與pytest-xdist相比:
pytest-xdist:
- 不是線程安全的埋市;
- 多線程時表現(xiàn)不佳冠桃;
- 需要state隔離;
pytest-parallel
- 線程安全的道宅;
- 可以對http請求使用非阻塞IO來使其具有高性能食听;
- 在Python環(huán)境中管理很少或沒有狀態(tài);
- 安裝:
pip install pytest-parallel
- 執(zhí)行:
# runs 2 workers with 1 test per worker at a time
$ pytest --workers 2
# runs 4 workers (assuming a quad-core machine) with 1 test per worker
$ pytest --workers auto
# runs 1 worker with 4 tests at a time
$ pytest --tests-per-worker 4
# runs 1 worker with up to 50 tests at a time
$ pytest --tests-per-worker auto
# runs 2 workers with up to 50 tests per worker
$ pytest --workers 2 --tests-per-worker auto