pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)

简介: pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)

1 使用场景

  • 为了排查某些问题,我们可能需要重复去执行某个用例进行问题分析;
  • 一些场景下,自动化测试时候某个用例时好时坏,为了排查这类问题,我们可能需要对用例进行重复执行。

2 pytest-repeat插件

2.1 环境要求

  • Python 2.7, 3.5+ 或 PyPy;
  • pytest 3.6或更高版本。

2.2 插件安装

pip3 install pytest-repeat

在这里插入图片描述

3 pytest-repeat使用

3.1 重复测试直到失败

  • pytest-x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止;
pytest --count=5 -x test_pytest_repeat.py
  • 比如以下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/2/28 
# 文件名称:test_pytest_repeat.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import random
import time
import pytest


def test_case01():
    computer = random.randint(0, 4)
    time.sleep(1)
    print(computer)
    assert computer < 3
  • 输出为:
test_pytest_repeat.py 
.0
.2
.3
F

================================================== FAILURES ==================================================
______________________________________________ test_case01[3-5] ______________________________________________

    def test_case01():
        computer = random.randint(0, 4)
        time.sleep(1)
        print(computer)
>       assert computer < 3
E       assert 3 < 3

test_pytest_repeat.py:18: AssertionError
========================================== short test summary info ===========================================
FAILED test_pytest_repeat.py::test_case01[3-5] - assert 3 < 3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================== 1 failed, 2 passed in 3.13s =========================================

3.2 用例标记执行重复多次

  • 使用 @pytest.mark.repeat(count) ,将代码中某些测试用例标记为执行重复多次;
  • 比如:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/2/28 
# 文件名称:test_pytest_repeat01.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest


@pytest.mark.repeat(8)
def test_case():
    print("测试用例执行")


if __name__ == '__main__':
    pytest.main(["-s", "test_pytest_repeat01.py"])
  • 输出为:
test_pytest_repeat01.py::test_case[1-8] PASSED                           [ 12%]测试用例执行

test_pytest_repeat01.py::test_case[2-8] PASSED                           [ 25%]测试用例执行

test_pytest_repeat01.py::test_case[3-8] PASSED                           [ 37%]测试用例执行

test_pytest_repeat01.py::test_case[4-8] PASSED                           [ 50%]测试用例执行

test_pytest_repeat01.py::test_case[5-8] PASSED                           [ 62%]测试用例执行

test_pytest_repeat01.py::test_case[6-8] PASSED                           [ 75%]测试用例执行

test_pytest_repeat01.py::test_case[7-8] PASSED                           [ 87%]测试用例执行

test_pytest_repeat01.py::test_case[8-8] PASSED                           [100%]测试用例执行


============================== 8 passed in 0.04s ==============================

3.3 命令行参数--repeat-scope详解

  • 命令行参数作用:可以覆盖默认的测试用例执行顺序,类似fixture的scope参数;
  • 说明:
作用范围 说明
function 默认,每个用例重复执行,再执行下一个用例
class 以class为单位,重复执行class里面的用例,再执行下一个
module 以模块为单位,重复执行模块里面的用例,再执行下一个
session 重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次

3.3.1 class示例

# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/1 
# 文件名称:test_pytest_repeat02.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest


class TestCase01():
    def test_01(self):
        print("假如我有一个亿,")

class TestCase02():
    def test_02(self):
        print("我一定带你去履行!")
  • 命令行执行:
pytest -s --count=3 --repeat-scope=class test_pytest_repeat02.py
  • 输出为:
test_pytest_repeat02.py 假如我有一个亿,
.假如我有一个亿,
.假如我有一个亿,
.我一定带你去履行!
.我一定带你去履行!
.我一定带你去履行!
.

=============== 6 passed in 0.16s ================

3.3.2 module示例

# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/1 
# 文件名称:test_pytest_repeat03.py
# 作用:pytest-repeat插件的使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest

def test_01():
    print("假如我有一个亿,")

def test_02():
    print("我一定带你去履行!")

class TestCase():
    def test_03(self):
        print("如果你有一个亿,可以先借我用用,我再带你去履行!")

  • 执行命令:
pytest -s --count=3 --repeat-scope=module test_pytest_repeat03.py
  • 输出为:
test_pytest_repeat03.py 假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.

================= 9 passed in 0.15s =================
目录
相关文章
|
11月前
|
测试技术
30-pytest-重复执行用例-pytest-repeat
30-pytest-重复执行用例-pytest-repeat
30-pytest-重复执行用例-pytest-repeat
|
10月前
|
测试技术 Python
pytest--运行指定的测试和参数化
pytest--运行指定的测试和参数化
|
11月前
|
测试技术
34-pytest-Hooks函数之获取用例执行结果
34-pytest-Hooks函数之获取用例执行结果
|
11月前
|
测试技术
22-pytest-allure.step()测试步骤描述
22-pytest-allure.step()测试步骤描述
|
11月前
|
测试技术
15-pytest-自定义用例执行顺序
15-pytest-自定义用例执行顺序
|
11月前
|
测试技术
16-pytest-skip跳过用例
16-pytest-skip跳过用例
|
测试技术
pytest 用例执行顺序
pytest 用例执行顺序
pytest 用例执行顺序
|
测试技术 Python
pytest学习和使用10-Pytest中的测试用例如何跳过执行?
pytest学习和使用10-Pytest中的测试用例如何跳过执行?
77 0
pytest学习和使用10-Pytest中的测试用例如何跳过执行?
pytest学习和使用19-pytest断言失败后,怎样保持后续的断言继续执行?(pytest-assume)
pytest学习和使用19-pytest断言失败后,怎样保持后续的断言继续执行?(pytest-assume)
97 0
|
测试技术
pytest学习和使用5-Pytest和Unittest中的断言如何使用?
pytest学习和使用5-Pytest和Unittest中的断言如何使用?
74 0
pytest学习和使用5-Pytest和Unittest中的断言如何使用?