zoukankan      html  css  js  c++  java
  • Pytest(6)重复运行用例pytest-repeat

    前言

    平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。
    自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次。
     

    安装

    pip3 install pytest-repeat
    

    命令行方式

    使用--count命令行选项来指定您要运行一个或多个测试的次数:

    $ pytest --count = 10 test2.py
    
    (pytest_env) ➜  testAPI pytest --count=10 test2.py                        
    Test session starts (platform: darwin, Python 3.7.6, pytest 6.2.1, pytest-sugar 0.9.4)
    rootdir: /Users/jkc/PycharmProjects/apiAutomatic, configfile: pytest.ini
    plugins: sugar-0.9.4, rerunfailures-9.1.1, html-3.1.1, metadata-1.11.0, cov-2.10.1, repeat-0.9.1, xdist-2.2.0, forked-1.3.0, allure-pytest-2.8.29
    collecting ... 
     testAPI/test2.py ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓                                                                                                                                                                   100% ██████████
    
    Results (0.12s):
          20 passed
    
    

    代码方式

    如果要在代码中将测试标记为重复多次,则可以使用@pytest.mark.repeat(count)装饰器:

    import pytest
    
    
    @pytest.mark.repeat(3)
    def test_example():
        print("执行成功")
    

    执行命令

    pytest -s test2.py
    

    执行结果

    collecting ... 执行成功
    
     testAPI/test2.py ✓                                                                                                                                                                                       33% ███▍      执行成功
     testAPI/test2.py ✓✓                                                                                                                                                                                      67% ██████▋   执行成功
     testAPI/test2.py ✓✓✓                                                                                                                                                                                    100% ██████████
    
    Results (0.08s):
           3 passed
    

    --repeat-scope

    作用:**可以覆盖默认的测试用例执行顺序,类似fixture的scope参数

    • function:默认,范围针对每个用例重复执行,再执行下一个用例
    • class:以class为用例集合单位,重复执行class里面的用例,再执行下一个
    • module:以模块为单位,重复执行模块里面的用例,再执行下一个
    • session:重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次
    def test_example():
        print("执行成功1")
    
    
    def test_example2():
        print("执行成功2")
    

    执行命令

    pytest -s --count=2 --repeat-scope=function test2.py
    

    执行结果

    collecting ... 执行成功1
    
     testAPI/test2.py ✓                                                                                                                                                                                       25% ██▌       执行成功1
     testAPI/test2.py ✓✓                                                                                                                                                                                      50% █████     执行成功2
     testAPI/test2.py ✓✓✓                                                                                                                                                                                     75% ███████▌  执行成功2
     testAPI/test2.py ✓✓✓✓                                                                                                                                                                                   100% ██████████
    
    Results (0.09s):
           4 passed
    

    兼容性问题

    不幸的是pytest-repeat不能与unittest.TestCase测试类一起使用。这些测试将始终仅运行一次,而不管是否--count显示并显示警告

    更多资料参考【官方文档:https://pypi.org/project/pytest-repeat/】

  • 相关阅读:
    Byobu(tmux)的使用与定制
    Centos 编译安装Python 2.6
    how to set up the remote accout who to connection and management mysql server
    CentOS安装python2.6以及MySQLpython
    byobu "屏风" 简介 ,以及在CentOS下安装及日常使用
    /bin/rm: cannot remove `libtoolT': No such file or directory
    MySQLpython EnvironmentError: mysql_config not found | InfoEntropy
    几个逼真的页面 watch out
    Emerge详细解释
    微软Internet TV初体验
  • 原文地址:https://www.cnblogs.com/jiakecong/p/14291700.html
Copyright © 2011-2022 走看看