ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-pytest学习(二十)-重复执行用例(pytest-repeat)

2020-05-12 10:02:57  阅读:279  来源: 互联网

标签:baidu __ repeat python py pytest test


一、前言

  平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。
  自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,或者针对某个模块的用例重复执行多次。 二、pytest-repeat   pytest-repeat是pytest的一个插件,用于重复执行单个用例,或多个测试用例,并指定重复次数,pytest-repeat支持的版本: (1)Python 2.7, 3.4+ 或 PyPy (2)py.test 2.8或更高
pip3 install pytest-repeat

  使用--count命令行选项指定要运行测试用例和测试次数

py.test --count=10 test_file.py

 

三、重复执行--count

web_conf_py是项目工程名称
│  conftest.py  
│  __init__.py
│              
├─baidu
│  │  conftest.py
│  │  test_1_baidu.py
│  │  test_2.py
│  │  __init__.py 
│          
├─blog
│  │  conftest.py
│  │  test_2_blog.py
│  │  __init__.py      

 

代码参考:

# web_conf_py/conftest.py
import pytest

@pytest.fixture(scope="session")
def start():
    print("\n打开首页")
    return "yoyo"

# web_conf_py/baidu/conftest.py
import pytest

@pytest.fixture(scope="session")
def open_baidu():
    print("打开百度页面_session")

# web_conf_py/baidu/test_1_baidu.py
import pytest
import time

def test_01(start, open_baidu):
    print("测试用例test_01")
    time.sleep(1)
    assert start == "yoyo"

def test_02(start, open_baidu):
    print("测试用例test_02")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_1_baidu.py"])


# web_conf_py/baidu/test_2.py
import pytest
import time

def test_06(start, open_baidu):
    print("测试用例test_06")
    time.sleep(1)
    assert start == "yoyo"
def test_07(start, open_baidu):
    print("测试用例test_07")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_2.py"])

  cmd进入到工程目录后,不带--count参数只会执行一次,加上参数--count=5,用例会重复执行5次。

pytest baidu/test_1_baidu.py -s --count=5

  从运行的用例结果看,是先重复5次test_01,再重复5次test_02,有时候我们希望执行的顺序是test_01,test_02按这样顺序重复五次,接下来就用到一个参数--repeat-scope

 

四、--repeat-scope

--repeat-scope类似于pytest fixturescope参数,--repeat-scope也可以设置参数: sessionmoduleclass或者function(默认值)

 

function(默认)范围针对每个用例重复执行,再执行下一个用例
class 以class为用例集合单位,重复执行class里面的用例,再执行下一个
module 以模块为单位,重复执行模块里面的用例,再执行下一个
session 重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等

使用--repeat-scope=session重复执行整个会话用例

五、@pytest.mark.repeat(count)   如果要在代码中标记要重复多次的测试,可以使用@pytest.mark.repeat(count)装饰器
# test_1_baidu.py
import pytest
import time

def test_01(start, open_baidu):
    print("测试用例test_01")
    time.sleep(0.5)
    assert start == "yoyo"

@pytest.mark.repeat(5)
def test_02(start, open_baidu):
    print("测试用例test_02")
    time.sleep(0.5)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_1_baidu.py"])

 

  这样执行用例时候,就不用带上--count参数,只针对test_02重复执行5次

E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23.2, repeat-0.7.0, metadata-1.7.0, html-1.19.0, forked-0.2
collected 6 items

baidu\test_1_baidu.py
打开首页
打开百度页面_session
测试用例test_01
.测试用例test_02
.测试用例test_02
.测试用例test_02
.测试用例test_02
.测试用例test_02
.

========================== 6 passed in 3.05 seconds ===========================

 

六、重复测试直到失败

  如果您正在尝试诊断间歇性故障,那么一遍又一遍地运行相同的测试直到失败是有用的。您可以将pytest的-x选项与pytest-repeat结合使用,以强制测试运行器在第一次失败时停止。例如:

py.test --count=1000 -x test_file.py

  这将尝试运行test_file.py 1000次,但一旦发生故障就会停止。

 

参考文章:https://www.jianshu.com/p/ddb0feb135ed

标签:baidu,__,repeat,python,py,pytest,test
来源: https://www.cnblogs.com/zhaocbbb/p/12874374.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有