17-Pytest如何重复执行用例?(pytest-repeat)
1 使用场景 2 pytest-repeat插件
3 pytest-repeat使用 3.1 重复测试直到失败 3.2 用例标记执行重复多次 3.3 命令行参数--repeat-scope详解 3.3.1 class示例 3.3.2 module示例
1 使用场景
为了排查某些问题,我们可能需要重复去执行某个用例进行问题分析; 一些场景下,自动化测试时候某个用例时好时坏,为了排查这类问题,我们可能需要对用例进行重复执行。
2 pytest-repeat插件
为了重复执行用例,我们可以使用pytest-repeat
插件; 详细参考官方教程:https://pypi.org/project/pytest-repeat/
2.1 环境要求
Python 2.7, 3.5+ 或 PyPy; pytest 3.6或更高版本。
2.2 插件安装
pip3 install pytest- repeat
3 pytest-repeat使用
3.1 重复测试直到失败
将pytest
的 -x
选项与pytest-repeat
结合使用,以强制测试运行程序在第一次失败时停止;
pytest - - count= 5 - x test_pytest_repeat. py
import random
import time
import pytest
def test_case01 ( ) :
computer = random. randint( 0 , 4 )
time. sleep( 1 )
print ( computer)
assert computer < 3
test_pytest_repeat. py
.0
.2
.3
F
== == == == == == == == == == == == == == == == == == == == == == == == == FAILURES == == == == == == == == == == == == == == == == == == == == == == == == ==
______________________________________________ test_case01[ 3 - 5 ] ______________________________________________
def test_case01 ( ) :
computer = random. randint( 0 , 4 )
time. sleep( 1 )
print ( computer)
> assert computer < 3
E assert 3 < 3
test_pytest_repeat. py: 18 : AssertionError
== == == == == == == == == == == == == == == == == == == == == short test summary info == == == == == == == == == == == == == == == == == == == == == =
FAILED test_pytest_repeat. py: : test_case01[ 3 - 5 ] - assert 3 < 3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
== == == == == == == == == == == == == == == == == == == == 1 failed, 2 passed in 3 . 13s == == == == == == == == == == == == == == == == == == == == =
3.2 用例标记执行重复多次
使用 @pytest.mark.repeat(count)
,将代码中某些测试用例标记为执行重复多次; 比如:
import pytest
@pytest. mark. repeat ( 8 )
def test_case ( ) :
print ( "测试用例执行" )
if __name__ == '__main__' :
pytest. main( [ "-s" , "test_pytest_repeat01.py" ] )
test_pytest_repeat01. py: : test_case[ 1 - 8 ] PASSED [ 12 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 2 - 8 ] PASSED [ 25 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 3 - 8 ] PASSED [ 37 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 4 - 8 ] PASSED [ 50 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 5 - 8 ] PASSED [ 62 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 6 - 8 ] PASSED [ 75 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 7 - 8 ] PASSED [ 87 % ] 测试用例执行
test_pytest_repeat01. py: : test_case[ 8 - 8 ] PASSED [ 100 % ] 测试用例执行
== == == == == == == == == == == == == == == 8 passed in 0 . 04s == == == == == == == == == == == == == == ==
3.3 命令行参数–repeat-scope详解
命令行参数作用:可以覆盖默认的测试用例执行顺序,类似fixture的scope
参数; 说明:
作用范围 说明 function
默认,每个用例重复执行,再执行下一个用例 class
以class为单位,重复执行class里面的用例,再执行下一个 module
以模块为单位,重复执行模块里面的用例,再执行下一个 session
重复整个测试会话,即所有测试用例的执行一次,然后再执行第二次
3.3.1 class示例
import pytest
class TestCase01 ( ) :
def test_01 ( self) :
print ( "假如我有一个亿," )
class TestCase02 ( ) :
def test_02 ( self) :
print ( "我一定带你去履行!" )
pytest - s - - count= 3 - - repeat- scope= class test_pytest_repeat02 . py
test_pytest_repeat02. py 假如我有一个亿,
. 假如我有一个亿,
. 假如我有一个亿,
. 我一定带你去履行!
. 我一定带你去履行!
. 我一定带你去履行!
.
== == == == == == == = 6 passed in 0 . 16s == == == == == == == ==
3.3.2 module示例
import pytest
def test_01 ( ) :
print ( "假如我有一个亿," )
def test_02 ( ) :
print ( "我一定带你去履行!" )
class TestCase ( ) :
def test_03 ( self) :
print ( "如果你有一个亿,可以先借我用用,我再带你去履行!" )
pytest - s - - count= 3 - - repeat- scope= module test_pytest_repeat03. py
test_pytest_repeat03. py 假如我有一个亿,
. 我一定带你去履行!
. 如果你有一个亿,可以先借我用用,我再带你去履行!
. 假如我有一个亿,
. 我一定带你去履行!
. 如果你有一个亿,可以先借我用用,我再带你去履行!
. 假如我有一个亿,
. 我一定带你去履行!
. 如果你有一个亿,可以先借我用用,我再带你去履行!
.
== == == == == == == == = 9 passed in 0 . 15s == == == == == == == == =