Pytest用例自定义 - 重复、并行、串行

简介:面对快速迭代和持续交付的需求,提高测试效率变得至关重要。并行测试因其显著的时间节省优势而备受青睐。然而,并非所有测试都适合并行执行。在某些情况下,串行执行是必要的,以确保测试的正确性和稳定性。本文将探讨在自动化测试中部分串行、部分并行执行的必要性和实践方法,帮助测试开发者在提高测试效率的同时,确保测试结果的可靠性和准确性。

历史攻略:

pytest运行结果解析及其改造

pytest+allure安装和使用

pytest:并行和并发运行用例

python:消息推送 - 企业微信机器人推送

让你的pytest日志更有序:配置和使用技巧

Python配置与测试利器:Hydra + pytest的完美结合

Playwright - 04:pytest并行、并发、运行浏览器兼容性测试

安装:

pip install pytest-xdist
pip install pytest-repeat

使用案例:main.py

# -*- coding: utf-8 -*-
# time: 2024/03/24 10:30
# file: main.py
# 公众号: 玩转测试开发
import os
import pytest

if __name__ == "__main__":
    # step-1:use pytest run test_case
    pytest.main(["-s", "test_case", '-n', 'auto', "--alluredir", "./report"])

    # step-2:auto report json data,zip to allure-html
    os.system("allure serve report")

test_case/test_demo.py

# -*- coding: utf-8 -*-
# time: 2024/3/22 0:34
# file: test_demo.py
# 公众号: 玩转测试开发
import time
import unittest


class TestDemo(unittest.TestCase):
    def test_01(self):
        time.sleep(5)
        print('测试用例1执行')

    def test_02(self):
        time.sleep(5)
        print('测试用例2执行')

    def test_03(self):
        time.sleep(5)
        print('测试用例3执行')

test_case/test_parallel.py

# -*- coding: utf-8 -*-
# time: 2024/3/24 12:16
# file: test_parallel.py
# 公众号: 玩转测试开发
import time
import pytest


class TestParallel:
    @pytest.mark.repeat(2)
    def test_parallel_1(self):
        time.sleep(5)
        print("Parallel Test 1")

    @pytest.mark.repeat(3)
    def test_parallel_2(self):
        time.sleep(5)
        print("Parallel Test 2")

    @pytest.mark.repeat(1)
    def test_parallel_3(self):
        time.sleep(5)
        print("Parallel Test 3")

    @pytest.mark.repeat(3)
    def test_parallel_4(self):
        time.sleep(5)
        print("Parallel Test 4")

test_case/test_serial.py

# -*- coding: utf-8 -*-
# time: 2024/3/24 12:16
# file: test_serial.py
# 公众号: 玩转测试开发
import time
import pytest


@pytest.mark.serial
class TestSerial:
    def test_serial_1(self):
        time.sleep(5)
        print("Serial Test 1")

    def test_serial_2(self):
        time.sleep(5)
        print("Serial Test 2")

配置文件:pytest.ini

# pytest.ini
[pytest]
markers =
    serial: mark test to run serially

运行结果:

图片

图片

注意事项:

1、同一文件中混合串行和并行测试,并且需要更精细的控制,可能需要编写更复杂的自定义逻辑或查找是否有现成的插件能满足这种特定的需求。

2、不关心执行顺序,简单地将 -n 参数设置为 auto,让 pytest-xdist 插件自动管理并行执行,通常是最简单也是最有效的方法。

结论:适当拆分错开并行和串行的测试类,通过pytest-xdist auto参数,可以便捷有效的大幅度提升运行测试用例的效率。

相关推荐

  1. pytest并发执行方案

    2024-04-26 12:46:01       35 阅读
  2. python——pytest对于失败的重新执行

    2024-04-26 12:46:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 12:46:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 12:46:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 12:46:01       87 阅读
  4. Python语言-面向对象

    2024-04-26 12:46:01       96 阅读

热门阅读

  1. Vue3全局Api应用实例

    2024-04-26 12:46:01       33 阅读
  2. 资源、死锁、如何监测死锁

    2024-04-26 12:46:01       41 阅读
  3. 树莓派在人工智能领域的应用探索

    2024-04-26 12:46:01       32 阅读
  4. C语言经典例题-11

    2024-04-26 12:46:01       33 阅读
  5. 【Go】通道作为函数参数

    2024-04-26 12:46:01       32 阅读
  6. C++ 内存管理

    2024-04-26 12:46:01       30 阅读
  7. 白帽子讲Web安全读书笔记

    2024-04-26 12:46:01       31 阅读
  8. AI电销机器人系统源码部署之:freeswitch安装Linux

    2024-04-26 12:46:01       33 阅读