在编写自动化脚本时,一般都要求遵循用例之间是独立的,期望在最终连跑验证的时候能随机执行,因为随机执行更能模拟手工测试的场景。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>