pytest常用插件推荐
2019-05-29 本文已影响34人
利欧leo丶
之前用到的一些pytest插件,在这记录一下。
pytest-sugar
- 作用:改变pytest执行的默认外观。
- 安装:
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默认的执行顺序;
- 安装:
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
本来按顺序执行应该是先执行test_one
然后再执行test_two
,但是加了@pytest.mark.run(order=1)
之后用例的执行顺序变了:
$ 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,仅仅执行你已经修改但是还没提交的测试用例;
- 安装:
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)
可以看到没有执行的为api.py
, tests/api/
, tests/test_board.py
.
此时执行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
- 作用:并发执行测试用例;
- 安装:
pip install pytest-xdist
- 用法:
pytest -n NUM
NUM表示执行的CPU数量,也可以使用auto
来自动识别系统的cpu数量并执行;
如果一个用例失败导致python解释器crash掉,pytest-xdist
会自动重启该解释器所在的worker并且正常报告错误,你可以使用--max-worker-restart
选项来限制可以重新启动的worker,或者使用--max-worker-restart=0
完全禁用重新启动。
pytest-parallel
- 作用:使用多处理(并行)和多线程(并发)快速运行测试;
- 与pytest-xdist相比:
pytest-xdist:
- 不是线程安全的;
- 多线程时表现不佳;
- 需要state隔离;
pytest-parallel
- 线程安全的;
- 可以对http请求使用非阻塞IO来使其具有高性能;
- 在Python环境中管理很少或没有状态;
- 安装:
pip install pytest-parallel
- 执行:
# 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