要重复执行测试用例,需要先安装pytest-repeat插件
全局安装: 即安装在全局环境中,新创建的工程导入全局环境时会将该包导入,cmd输入:pip install pytest
-repeat,安装成功后会显示repeat版本号
局部安装: 即安装在当前项目的环境下,新创建的工程不会同步该包,在PyCharm→File→setting,搜索pytest
intrepreter,点击“+”号,如下图所示:
image.png
再搜索pytest-repeat,点击install package安装
image.png
出现下图表示安装成功:
image.png
重复执行测试用例的方法有多种,比如:方法一的重复执行单条测试用例;方法二的重复执行每条测试用例;方法三的每条测试用例执行一次,共执行N遍。
在测试用例前添加注解@pytest.mark.repeat(value),value表示重复的次数,来实现单条用例的重复执行。
import pytest
class Test_Pytest:
@pytest.mark.repeat(2)
def test_one(self):
print("test_one方法执行")
def test_two(self):
print("test_two方法执行")
运行结果为:被装饰的测试用例test_one会连续执行两次,再执行test_two,即按照顺序,第一个测试用例执行N次,再执行下一个测试用例
image.png
在终端传入-count的方式实现重复执行测试用例
import pytest
class Test_Pytest:
def test_one(self):
print("test_one方法执行")
def test_two(self):
print("test_two方法执行")
# 在终端(terminal)输入:
pytest -s -v --count=2 test_Pytest.py
运行结果为:测试用例test_one会连续执行两次,再执行两次test_two,即按照执行顺序,每一个测试用例都执行N次
image.png
注意:-s:表示输出用例中的调式信息,比如print的打印信息等。
-v:表示输出用例更加详细的执行信息,比如用例所在的文件及用例名称等。
-count即方法二的结果一样,但不影响其它功能的运行,所以这边先做个记录,后续有用到这个知识再去解决。有没有哪位大神,能帮忙解决一下,感谢!!!)
-repeat-scope类似于pytest fixture的scope参数,在终端输入,-repeat-scope有四个参数:
重运行机制使用到了pytest的插件,插件名称为:rerunfailures,要使用它,需要先安装此插件
命令行输入:pip install pytest-rerunfailures
出现下图表示安装成功:
image.png
import pytest
class TestFailure:
# 用例失败后重新运行2次,重运行之间的间隔时间为10s
@pytest.mark.flaky(reruns=2, reruns_delay=10)
def test_one(self):
a = 1 + 2
assert 1 == a
def test_two(self):
a = 1 + 2
assert 3 == a
运行结果如下:
image.png
注意:
# 用例失败后重新运行2次,重运行之间的间隔时间为10s
import pytest
class TestFailure:
def test_one(self):
a = 1 + 2
assert 1 == a
def test_two(self):
a = 1 + 2
assert 3 == a
# 在终端(terminal)输入:
pytest -s -v --reruns=2 --reruns-delay=10 test.py
运行结果如下:
image.png
将pytest的 -x 选项与pytest-repeat结合使用,可以实现在重复运行测试用例的过程中,测试用例第一次失败时就停止运行,具体实现方法如下:
# 重复运行5次,运行过程中第一次失败时就停止运行
import pytest
class TestFailure:
def test_one(self):
a = 1 + 2
assert 1 == a
def test_two(self):
a = 1 + 2
assert 3 == a
# 在终端(terminal)输入:
pytest -s -v --count=5 -x test.py
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。