Pytest如何随机执行用例

【原文链接】Pytest如何随机执行用例

在编写自动化脚本时,一般都要求遵循用例之间是独立的,期望在最终连跑验证的时候能随机执行,因为随机执行更能模拟手工测试的场景。Pytest框架通过提供第三方插件pytest-random-order。

首先需要执行如下命令安装第三方插件

pip install pytest-random-order

然后编写如下三个用例的测试脚本。

def test_01():
    print("in test01...")
    assert 1==1

def test_02():
    print("in test02...")
    assert 1==1

def test_03():
    print("in test03...")
    assert 1==1

然后使用 –radom-order 参数即可实现自动化脚本的随机执行,如下,通过打印结果可以发现先执行了test_03,然后执行了test_02,最后执行了test_01

(demo--ip5ZZo0) D:\demo>pytest -s --random-order
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Using --random-order-bucket=module
Using --random-order-seed=937307

rootdir: D:\demo
plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 3 items

test_demo.py in test03...
.in test02...
.in test01...
.

==================== 3 passed in 2.26s ====================

(demo--ip5ZZo0) D:\demo>

此外,pytest-random-order插件还支持设置一定范围内随机,通过 –random-order-bucket 参数设置范围,可设置的范围有module和class,当然还有其他选项,但是其他选项基本用不着,因此这里只介绍module和class两个选项。为了更好的演示随机性,下面重新编写自动化脚本如下,即一个测试文件中有类外的测试函数,有两个测试类,每个测试类中有三个测试方法。

def test_01():
    print("in test01...")
    assert 1==1

def test_02():
    print("in test02...")
    assert 1==1

def test_03():
    print("in test03...")
    assert 1==1

class TestDemo1():
    def test_04(self):
        print("in test04...")
        assert 1==1

    def test_05(self):
        print("in test05...")
        assert 1==1

    def test_06(self):
        print("in test06...")
        assert 1==1

class TestDemo2():
    def test_07(self):
        print("in test07...")
        assert 1==1

    def test_08(self):
        print("in test08...")
        assert 1==1

    def test_09(self):
        print("in test09...")
        assert 1==1

首先使用 –random-order 参数,即不指定随机范围的时候执行,执行结果如下,可以发现此时是所有用例都随机执行的。

(demo--ip5ZZo0) D:\demo>pytest -s --random-order
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Using --random-order-bucket=module
Using --random-order-seed=982647

rootdir: D:\demo
plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 9 items

test_demo.py in test05...
.in test04...
.in test02...
.in test03...
.in test07...
.in test06...
.in test09...
.in test01...
.in test08...
.

==================== 9 passed in 0.03s ====================

(demo--ip5ZZo0) D:\demo>

通过 –random-order-bucket=class 参数则可以指定每个类中的测试方法随机顺序执行,执行结果如下,此时可以做到一个类中的方法随机,这种执行顺序对测试类中有setup和teardown的时候是非常有益的。

(demo--ip5ZZo0) D:\demo>pytest -s --random-order-bucket=class
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Using --random-order-bucket=class
Using --random-order-seed=186060

rootdir: D:\demo
plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 9 items

test_demo.py in test01...
.in test02...
.in test03...
.in test07...
.in test08...
.in test09...
.in test05...
.in test04...
.in test06...
.

==================== 9 passed in 0.04s ====================

(demo--ip5ZZo0) D:\demo>

如下结果则设置模块内随机,这里因为只有一个测试文件,因此测试文件内所有用例随机执行了。当存在多个测试文件时,则每个测试文件内随机执行。

(demo--ip5ZZo0) D:\demo>pytest -s --random-order-bucket=module
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Using --random-order-bucket=module
Using --random-order-seed=91396

rootdir: D:\demo
plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 9 items

test_demo.py in test01...
.in test08...
.in test09...
.in test05...
.in test04...
.in test07...
.in test03...
.in test02...
.in test06...
.

==================== 9 passed in 0.04s ====================

(demo--ip5ZZo0) D:\demo>

在随机执行脚本的时候也有新的问题,比如第一次随机执行脚本,中间有脚本失败了,但是再重新随机执行一次时就又不会出现失败了,即某一些用例再一定的执行顺序下才会出现,那么这个时候定位问题是很难的,pytest-random-order插件另外提供了一种方式,即通过使用 –random-order-seed=N 的参数,当第二次再次执行指定同样的N值时,执行的顺序和前一次保持一致,比如第一执行指定N为10000,执行结果如下:

(demo--ip5ZZo0) D:\demo>pytest -s --random-order-seed=10000
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Using --random-order-bucket=module
Using --random-order-seed=10000

rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 9 items

test_demo.py in test06...
.in test08...
.in test04...
.in test05...
.in test07...
.in test03...
.in test09...
.in test02...
.in test01...
.

==================== 9 passed in 0.04s ====================

(demo--ip5ZZo0) D:\demo>

当再次使用 pytest -s --random-order-seed=10000 命令执行时,执行结果如下,可以发现此时的执行顺序和上一次执行完全一致,这样就很容易复现问题,方便定位问题了。

(demo--ip5ZZo0) D:\demo>pytest -s --random-order-seed=10000
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
Using --random-order-bucket=module
Using --random-order-seed=10000

rootdir: D:\demo
plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
collected 9 items

test_demo.py in test06...
.in test08...
.in test04...
.in test05...
.in test07...
.in test03...
.in test09...
.in test02...
.in test01...
.

==================== 9 passed in 0.04s ====================

(demo--ip5ZZo0) D:\demo>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值