前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)

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

原创
作者头像
虫无涯
发布2023-03-03 13:08:41
1.2K0
发布2023-03-03 13:08:41
举报
文章被收录于专栏:全栈测试技术全栈测试技术

1 使用场景

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

2 pytest-repeat插件

2.1 环境要求

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

2.2 插件安装

代码语言:python
复制
pip3 install pytest-repeat
在这里插入图片描述
在这里插入图片描述

3 pytest-repeat使用

3.1 重复测试直到失败

  • pytest-x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止;
代码语言:python
复制
pytest --count=5 -x test_pytest_repeat.py
  • 比如以下:
代码语言:python
复制
# -*- 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
  • 输出为:
代码语言:python
复制
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) ,将代码中某些测试用例标记为执行重复多次;
  • 比如:
代码语言:python
复制
# -*- 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"])
  • 输出为:
代码语言:python
复制
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示例

代码语言:python
复制
# -*- 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("我一定带你去履行!")
  • 命令行执行:
代码语言:python
复制
pytest -s --count=3 --repeat-scope=class test_pytest_repeat02.py
  • 输出为:
代码语言:python
复制
test_pytest_repeat02.py 假如我有一个亿,
.假如我有一个亿,
.假如我有一个亿,
.我一定带你去履行!
.我一定带你去履行!
.我一定带你去履行!
.

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

3.3.2 module示例

代码语言:python
复制
# -*- 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("如果你有一个亿,可以先借我用用,我再带你去履行!")
  • 执行命令:
代码语言:python
复制
pytest -s --count=3 --repeat-scope=module test_pytest_repeat03.py
  • 输出为:
代码语言:python
复制
test_pytest_repeat03.py 假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.假如我有一个亿,
.我一定带你去履行!
.如果你有一个亿,可以先借我用用,我再带你去履行!
.

================= 9 passed in 0.15s =================

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 使用场景
  • 2 pytest-repeat插件
    • 2.1 环境要求
      • 2.2 插件安装
      • 3 pytest-repeat使用
        • 3.1 重复测试直到失败
          • 3.2 用例标记执行重复多次
            • 3.3 命令行参数--repeat-scope详解
              • 3.3.1 class示例
              • 3.3.2 module示例
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档