pip install pytest-repeat
示例代码
import pytest
@pytest.mark.repeat(3)
def test_a01():
assert 1==2
if __name__ == '__main__':
pytest.main(['-sv',__file__])
示例输出
collecting ... collected 3 items
test_demo.py::test_a01[1-3] FAILED
test_demo.py::test_a01[2-3] FAILED
test_demo.py::test_a01[3-3] FAILED
================================== FAILURES ===================================
________________________________ test_a01[1-3] ________________________________
@pytest.mark.repeat(3)
def test_a01():
> assert 1==2
E assert 1 == 2
test_demo.py:4: AssertionError
________________________________ test_a01[2-3] ________________________________
@pytest.mark.repeat(3)
def test_a01():
> assert 1==2
E assert 1 == 2
test_demo.py:4: AssertionError
________________________________ test_a01[3-3] ________________________________
@pytest.mark.repeat(3)
def test_a01():
> assert 1==2
E assert 1 == 2
test_demo.py:4: AssertionError
=========================== short test summary info ===========================
FAILED test_demo.py::test_a01[1-3] - assert 1 == 2
FAILED test_demo.py::test_a01[2-3] - assert 1 == 2
FAILED test_demo.py::test_a01[3-3] - assert 1 == 2
============================== 3 failed in 0.10s ==============================
从结果看,重复插件的使用会让你的用例变成多个(这点未必是你想要的,要注意)
语法
pytest --count=3 test_demo.py
示例代码
import pytest
def test_a01():
assert 1==2
if __name__ == '__main__':
pytest.main(['-sv','--count=5',__file__])
输出跟上面第一个用法的是一样的
但装饰器是要装饰在每个测试用例上的,而命令行的做法就是一把梭,全部运行多次。
如果我们要对多个测试函数进行重复运行,要么加多个装饰器,要么用命令行参数
但是上述两种方法都是A重复,B重复这样,无法做到AB-AB-AB的模式
如果要实现组合重复运行,那就要用到该插件提供的另外一个参数--repeat-scope
--repeat-scope类似于pytest fixture的scope参数,--repeat-scope也可以设置参数: session
, module
,class
或者function
(默认值)
function
(默认)范围针对每个用例重复执行,再执行下一个用例class
以class为用例集合单位,重复执行class里面的用例,再执行下一个module
以模块为单位,重复执行模块里面的用例,再执行下一个session
重复整个测试会话,即所有收集的测试执行一次,然后所有这些测试再次执行等等示例代码1(不加repeat-scope):A运行2次,B运行2次
import pytest
def test_a():
assert 1==2
def test_b():
assert 1==2
if __name__ == '__main__':
pytest.main(['-sv','--count=2',__file__])
#运行结果 AABB
=========================== short test summary info ===========================
FAILED test_demo.py::test_a[1-2] - assert 1 == 2
FAILED test_demo.py::test_a[2-2] - assert 1 == 2
FAILED test_demo.py::test_b[1-2] - assert 1 == 2
FAILED test_demo.py::test_b[2-2] - assert 1 == 2
============================== 4 failed in 0.11s ==============================
示例代码(加repeat-scope):A-B运行2次
import pytest
def test_a():
assert 1==2
def test_b():
assert 1==2
if __name__ == '__main__':
pytest.main(['-sv','--count=2','--repeat-scope=module',__file__])
# ABAB
=========================== short test summary info ===========================
FAILED test_demo.py::test_a[1-2] - assert 1 == 2
FAILED test_demo.py::test_b[1-2] - assert 1 == 2
FAILED test_demo.py::test_a[2-2] - assert 1 == 2
FAILED test_demo.py::test_b[2-2] - assert 1 == 2
============================== 4 failed in 0.11s ==============================
如果--repeat-scope=session在此处的效果是一样的
这个插件的--repeat-scope=并没有同步fixture的scope(多了个package)
Pytest-repeat.py的部分源码
def pytest_addoption(parser):
parser.addoption(
'--count',
action='store',
default=1,
type=int,
help='Number of times to repeat each test')
parser.addoption(
'--repeat-scope',
action='store',
default='function',
type=str,
choices=('function', 'class', 'module', 'session'),
help='Scope for repeating tests')