赞助

pytest——重复执行

一、重复执行用例repeat

1.安装:pip install pytest-repeat

2.执行:pytest  test01.py --count=5

1 def test_a():
2     print('i love you python')
3 
4 def test_b():
5     a='hello'
6     assert 'h' in a,'恭喜你答错了'

platform win32 -- Python 3.7.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: D:\PycharmProjects\haha\pytest_learn
plugins: allure-pytest-2.7.0, forked-1.0.2, html-1.21.1, metadata-1.8.0, repeat-0.8.0, xdist-1.29.0
collected 10 items


test_01.py i love you python
.i love you python
.i love you python
.i love you python
.i love you python
......


=============================================== 10 passed in 0.03 seconds ================================================

3.也可以设置模块或者类等执行次数:--repeat-scope,类似于fixture的scope,可以设置参数session,module,class,function(默认值)

1 pytest baidu/test01.py -s --count=5 --repeat-scope=session

4.@pytest.mark.repeat(count),直接标记某个用例执行多少次

1 import pytest
2 
3 @pytest.mark.repeat(5)              #test_a用例执行5次
4 def test_a():
5     print('i love you python')
6 
7 def test_b():
8     a='hello'
9     assert 'h' in a,'恭喜你答错了'

5.有时候,功能模块不稳定,间接性的出现错误,可以将-x和pytest-repeat一起用,强制运行器在第一次测试失败时停止:

1 pytest -s --count=1000 -x test_file.py

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

 

posted on 2019-09-02 14:11  小二哥很二  阅读(2413)  评论(0编辑  收藏  举报