环境前提:
只有同时满足一下先决条件才能使用pytest-rerunfailures
①python的版本不能过低;
②pytest 5.0或更高版本;
背景:
平时在做接口测试的时候,经常会遇到网络抖动或者环境问题导致测试用例运行失败,而这个并不是我们想要的结果;
我们想要重新运行失败的测试用例,这个就需要通过插件pytest-rerunfailures来实现了。
安装插件pytest-rerunfailures
<span style="color:#111111"><span style="background-color:#ffffff"><code class="language-mipsasm">pip <span style="color:#0000ff">install </span>pytest-rerunfailures
</code></span></span>
①执行命令重新执行失败的测试用例:使用 --reruns 命令行参数选项,并指定要运行测试的最大次数:
<span style="color:#111111"><span style="background-color:#ffffff"><code class="language-bash">pytest test_add.py --reruns NUM <span style="color:#008000"># NUM表示重试的次数</span>
</code></span></span>
【注意】重复运行失败的测试用例时,对应的fixture或者setup函数也会重新执行(例如:scope参数为method的fixture前置函数)
举例:
代码参考如下:
<span style="color:#111111"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000"># file_name: test_add.py</span>
<span style="color:#0000ff">import</span> pytest
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_add01</span>():
<span style="color:#0000ff">print</span>(<span style="color:#a31515">"----------------->>> test_add01"</span>)
<span style="color:#0000ff">assert</span> <span style="color:#880000">1</span>
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_add02</span>():
<span style="color:#0000ff">print</span>(<span style="color:#a31515">"----------------->>> test_add02"</span>)
<span style="color:#0000ff">assert</span> <span style="color:#880000">0</span>
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_add03</span>():
<span style="color:#0000ff">print</span>(<span style="color:#a31515">"----------------->>> test_add03"</span>)
<span style="color:#0000ff">assert</span> <span style="color:#880000">1</span>
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_add04</span>():
<span style="color:#0000ff">print</span>(<span style="color:#a31515">"----------------->>> test_add04"</span>)
<span style="color:#0000ff">assert</span> <span style="color:#880000">1</span>
<span style="color:#0000ff">if</span> __name__ == <span style="color:#a31515">'__main__'</span>:
pytest.main([<span style="color:#a31515">"-s"</span>, <span style="color:#a31515">"test_add.py"</span>])
</code></span></span>
执行命令:pytest ./pytest_study/test_add.py --reruns 2 -s
(NUM=2表示失败测试用例重试2次,上述代码中只有test_add02()方法会失败)
注意 :
pytest多种运行模式支持叠加执行:
例如同时运行四个进程,且失败后重跑2次,pytest命令行运行:pytest -n 4 -reruns 2
②设置添加重新执行的延时时间并执行失败的测试用例
要在两次重试之间增加延迟时间,使用 --reruns-delay
命令行选项,指定下次测试重新开始之前等待的秒数:
<span style="color:#111111"><span style="background-color:#ffffff"><code class="language-css">pytest <span style="color:#ff0000">--reruns</span> <span style="color:#880000">5</span> <span style="color:#ff0000">--reruns-delay</span> <span style="color:#880000">10</span>
</code></span></span>
③重新运行指定的测试用例:测试用例失败重跑的装饰器用法
添加flaky装饰器 @pytest.mark.flaky(reruns=5)
,并指定最大重新运行次数。
示例代码如下:
<span style="color:#111111"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#008000">#-*- coding: utf-8 -*-</span>
<span style="color:#0000ff">import</span> random
<span style="color:#0000ff">import</span> pytest
<span style="color:#2b91af">@pytest.mark.flaky(reruns=<span style="color:#880000">5</span>)</span>
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_retry1</span>():
n = random.randint(<span style="color:#880000">0</span>, <span style="color:#880000">9</span>)
<span style="color:#0000ff">print</span>(<span style="color:#a31515">f"\n 输出随机数: {n} "</span>)
<span style="color:#0000ff">assert</span> n == <span style="color:#880000">2</span>
<span style="color:#2b91af">@pytest.mark.flaky(reruns=<span style="color:#880000">5</span>)</span>
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_retry2</span>():
<span style="color:#0000ff">assert</span> random.choice([<span style="color:#a31515">True</span>, <span style="color:#a31515">False</span>, <span style="color:#a31515">False</span>])
</code></span></span>
运行结果:【注意】测试结果以最后一次重新执行测试用例的结果为最终结果,即用例执行被判定为FAILED或者PASSED
第一个测试方法:
第二个测试方法:
④对单个测试用例设置重新运行等待延迟时间
实例代码:
<span style="color:#111111"><span style="background-color:#ffffff"><code class="language-python"><span style="color:#2b91af">@pytest.mark.flaky(reruns=<span style="color:#880000">5</span>,reruns_delay=<span style="color:#880000">2</span>)</span>
<span style="color:#0000ff">def</span> <span style="color:#a31515">test_retry1</span>():
n = random.randint(<span style="color:#880000">0</span>, <span style="color:#880000">9</span>)
<span style="color:#0000ff">print</span>(<span style="color:#a31515">f"\n 输出随机数: {n} "</span>)
<span style="color:#0000ff">assert</span> n == <span style="color:#880000">2</span>
</code></span></span>
运行结果:
【注意】
1、如果使用装饰器的方式指定了测试用例的重新运行次数,则在命令行参数中添加–reruns对这些测试用例是不会生效的。
2、兼容性问题:
- 不可以和fixture装饰器一起使用: @pytest.fixture()
- 该插件与pytest-xdist的 --looponfail 标志不兼容
- 该插件与核心–pdb标志不兼容.
这可能是B站最详细的pytest自动化测试框架教程,整整100小时,全程实战!!!