python进阶课

pytest常用插件推荐

2019-05-29  本文已影响34人  利欧leo丶

之前用到的一些pytest插件,在这记录一下。

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

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 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

@pytest.mark.flaky(reruns=5)
def test_example():
    import random
    assert random.choice([True, False])

pytest-xdist

pytest-parallel

pytest-xdist
  1. 不是线程安全的;
  2. 多线程时表现不佳;
  3. 需要state隔离;
pytest-parallel
  1. 线程安全的;
  2. 可以对http请求使用非阻塞IO来使其具有高性能;
  3. 在Python环境中管理很少或没有状态;
# 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
上一篇下一篇

猜你喜欢

热点阅读