重复执行repeat

前言:

在测试过程中,会有一些bug是偶然出现,需要多次测试进行复现。在自动化测试过程中也存在这样的情况,为了能使bug复现,需要对用例进行多次重复运行。

在pytest中,pytest-repeat插件可以提供这样的功能。

 

一、重复跑

import random


class TestC:
    def test_c_1(self):
        print("this is test_c_1")
        a = random.choice([1, 3, 4, 66, 12, 43, 12])
        assert a == 4

    def test_c_2(self):
        print("this is test_c_2")
        a = random.choice([1, 3, 4, 66, 12, 43, 12])
        assert a == 3

程序分析:random.choice([1, 3, 4, 66, 12, 43, 12])表示:a是从[1, 3, 4, 66, 12, 43, 12]随机抽取的一个元素。

然后对a进行断言。

执行命令:

pytest -s --count 5

执行结果:

 

 

 我们可以看到,每条用例执行了5次,共执行了10次。

 

二、失败停止

在实际应用中,可能不需要用例重复跑固定的次数,而是需要失败后就停止重复执行,结合-x便可以实现。

import random


class TestC:
    def test_c_1(self):
        print("this is test_c_1")
        a = random.choice([1, 3, 4])
        assert a == 4

    def test_c_2(self):
        print("this is test_c_2")
        a = random.choice([1, 3, 4])
        assert a == 3

执行命令:

pytest -s -x --count 5

执行结果:

 

 我们可以看到,当遇到运行失败后,程序停止运行。

posted @ 2022-03-07 18:47  Target_L  阅读(87)  评论(0)    收藏  举报