遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(31):pytest常用插件 - 并发执行(pytest-xdist)

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

应用条件

无依赖:用例间没有关系

无顺序:用例可以不按顺序随机执行

此时,就可以并发执行,节约测试时间

注意:并发执行会打乱执行顺序,与pytest-ordering插件是冲突的

 

插件安装

pip install pytest-xdist

 

使用方式

加参数-n x,x表示进程数(pytest-xdist是进程级并发)

也可以:-n auto,可以根据当前系统的CPU核数自动设置进程数

执行原理过程类似jmeter分布式

 

基础示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest


class Test01:

    def test_d(self):
        print("---test_d")

    @pytest.mark.run(order=-3)
    def test_c(self):
        print("---test_c")

    @pytest.mark.run(order=0)
    def test_b(self):
        print("---test_b")

    @pytest.mark.run(order=1)
    def test_a(self):
        print("---test_a")

 

非并发执行

 

2个进程并发执行:gw0、gw1

 

3个进程并发执行:gw0、gw1、gw2

 

自动进程并发执行:gw0、gw1、gw2、gw3

 

分组执行

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/


class Test01:
    def test_d(self):
        print("---test_d")

    def test_c(self):
        print("---test_c")

class Test02:
    def test_b(self):
        print("---test_b")

    def test_a(self):
        print("---test_a")

  

不分组:test_a和test_d分别属于不同的测试类,但是都是被gw0执行

 

分组:哪怕指定了3个进程,但是实际是2个,分别执行不同测试类下的两个方法

 

更多实践应用

https://www.cnblogs.com/uncleyong/p/18032317

 

【bak】

 

posted @ 2024-02-24 10:32  全栈测试笔记  阅读(134)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end