Pytest----如何随机执行用例
作者:redrose2100   类别:    日期:2022-09-28 02:50:45    阅读:1105 次   消耗积分:0 分

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

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

    1. pip install pytest-random-order

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

    1. def test_01():
    2. print("in test01...")
    3. assert 1==1
    4. def test_02():
    5. print("in test02...")
    6. assert 1==1
    7. def test_03():
    8. print("in test03...")
    9. assert 1==1

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

    1. (demo--ip5ZZo0) D:\demo>pytest -s --random-order
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
    4. Using --random-order-bucket=module
    5. Using --random-order-seed=937307
    6. rootdir: D:\demo
    7. plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
    8. collected 3 items
    9. test_demo.py in test03...
    10. .in test02...
    11. .in test01...
    12. .
    13. ==================== 3 passed in 2.26s ====================
    14. (demo--ip5ZZo0) D:\demo>

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

    1. def test_01():
    2. print("in test01...")
    3. assert 1==1
    4. def test_02():
    5. print("in test02...")
    6. assert 1==1
    7. def test_03():
    8. print("in test03...")
    9. assert 1==1
    10. class TestDemo1():
    11. def test_04(self):
    12. print("in test04...")
    13. assert 1==1
    14. def test_05(self):
    15. print("in test05...")
    16. assert 1==1
    17. def test_06(self):
    18. print("in test06...")
    19. assert 1==1
    20. class TestDemo2():
    21. def test_07(self):
    22. print("in test07...")
    23. assert 1==1
    24. def test_08(self):
    25. print("in test08...")
    26. assert 1==1
    27. def test_09(self):
    28. print("in test09...")
    29. assert 1==1

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

    1. (demo--ip5ZZo0) D:\demo>pytest -s --random-order
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
    4. Using --random-order-bucket=module
    5. Using --random-order-seed=982647
    6. rootdir: D:\demo
    7. plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
    8. collected 9 items
    9. test_demo.py in test05...
    10. .in test04...
    11. .in test02...
    12. .in test03...
    13. .in test07...
    14. .in test06...
    15. .in test09...
    16. .in test01...
    17. .in test08...
    18. .
    19. ==================== 9 passed in 0.03s ====================
    20. (demo--ip5ZZo0) D:\demo>

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

    1. (demo--ip5ZZo0) D:\demo>pytest -s --random-order-bucket=class
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
    4. Using --random-order-bucket=class
    5. Using --random-order-seed=186060
    6. rootdir: D:\demo
    7. plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
    8. collected 9 items
    9. test_demo.py in test01...
    10. .in test02...
    11. .in test03...
    12. .in test07...
    13. .in test08...
    14. .in test09...
    15. .in test05...
    16. .in test04...
    17. .in test06...
    18. .
    19. ==================== 9 passed in 0.04s ====================
    20. (demo--ip5ZZo0) D:\demo>

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

    1. (demo--ip5ZZo0) D:\demo>pytest -s --random-order-bucket=module
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
    4. Using --random-order-bucket=module
    5. Using --random-order-seed=91396
    6. rootdir: D:\demo
    7. plugins: forked-1.4.0, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
    8. collected 9 items
    9. test_demo.py in test01...
    10. .in test08...
    11. .in test09...
    12. .in test05...
    13. .in test04...
    14. .in test07...
    15. .in test03...
    16. .in test02...
    17. .in test06...
    18. .
    19. ==================== 9 passed in 0.04s ====================
    20. (demo--ip5ZZo0) D:\demo>

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

    1. (demo--ip5ZZo0) D:\demo>pytest -s --random-order-seed=10000
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
    4. Using --random-order-bucket=module
    5. Using --random-order-seed=10000
    6. rootdir: D:\demo
    7. plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
    8. collected 9 items
    9. test_demo.py in test06...
    10. .in test08...
    11. .in test04...
    12. .in test05...
    13. .in test07...
    14. .in test03...
    15. .in test09...
    16. .in test02...
    17. .in test01...
    18. .
    19. ==================== 9 passed in 0.04s ====================
    20. (demo--ip5ZZo0) D:\demo>

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

    1. (demo--ip5ZZo0) D:\demo>pytest -s --random-order-seed=10000
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.1.3, pluggy-1.0.0
    4. Using --random-order-bucket=module
    5. Using --random-order-seed=10000
    6. rootdir: D:\demo
    7. plugins: forked-1.4.0, picked-0.4.6, random-order-1.0.4, repeat-0.9.1, xdist-2.5.0
    8. collected 9 items
    9. test_demo.py in test06...
    10. .in test08...
    11. .in test04...
    12. .in test05...
    13. .in test07...
    14. .in test03...
    15. .in test09...
    16. .in test02...
    17. .in test01...
    18. .
    19. ==================== 9 passed in 0.04s ====================
    20. (demo--ip5ZZo0) D:\demo>
    始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
    版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/402
    个人成就
    • 博客总数: 613 
    • 阅读总量: 762450 
    • 2022年 : 371 篇 
    • 2023年 : 211 篇 
    • 2024年 : 31 篇 
    • 2025年 : 0 篇 
    测试开发技术全栈公众号
    DevOps技术交流微信群