pytest的插件有很多,主要是给我们各种场景下使用。
1.pytest之插件pytest-xdist
场景:假设我们有1000个用例,一个用例执行1分钟,那么就需要1000分钟,这样就会大大的增加测试时间,所以这1000个用例我们就在几台机器上或者几个同事去分摊执行这些用例,这样缩短执行的时间。
解决:pytest-xdist就可以解决这样的问题,并发执行或分布式执行测试用例。pytest分布式执行插件:pytest-xdist。
前提:测试用例之间没有先后顺序,可以并行执行。
安装:pip install pytest-xdist
使用:在多个CPU并行执行用例,直接加 -n 3 是并行数量 pytest xxx.py -n 3
2.pytest之插件调整测试用例顺序pytest-ordering
场景:我们的用例一般都是按顺序执行的,比如有一个用例我想第一个执行,最后一个执行,或者其中几个执行。
安装:pip install pytest-ordering
使用:添加装饰器
@pytest.mark.latest
@pytest.mark.first
@pytest.mark.run(order=1)
举例:
import pytest
import xdist
import pytest_ordering
import time
# @pytest.mark.parametrize("x",list(range(10)))
# def test_x(x):
# time.sleep(1)
@pytest.mark.last
def test_one():
time.sleep(1)
@pytest.mark.first
def test_two():
time.sleep(1)
@pytest.mark.run(order=1)
def test_three():
time.sleep(1)
@pytest.mark.run(order=2)
def test_three1():
time.sleep(1)
if __name__ == '__main__':
pytest.main(['-s','-v','test_xdist.py'])
返回的内容:
collecting ... collected 4 items
test_xdist.py::test_two PASSED
test_xdist.py::test_three PASSED
test_xdist.py::test_three1 PASSED
test_xdist.py::test_one PASSED