一、显示执行最慢的N个用例
(1)在test_demo.py中编写10个测试函数
import time
def test_demo01():
time.sleep(0.1)
assert 1==1
def test_demo02():
time.sleep(0.2)
assert 1==1
def test_demo03():
time.sleep(0.3)
assert 1==1
def test_demo04():
time.sleep(0.4)
assert 1==1
def test_demo05():
time.sleep(0.5)
assert 1==1
def test_demo06():
time.sleep(0.6)
assert 1==1
def test_demo07():
time.sleep(0.7)
assert 1==1
def test_demo08():
time.sleep(0.8)
assert 1==1
def test_demo09():
time.sleep(0.9)
assert 1==1
def test_demo10():
time.sleep(1.0)
assert 1==1
def test_demo11():
assert 1==1
(2) 使用命令 pytest --durations=5 执行用来显示执行最慢的5个用例
$ pytest --durations=5
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items
test_demo01.py ........... [100%]
========================================================================= slowest 5 durations ==========================================================================
1.00s call test_demo01.py::test_demo10
0.92s call test_demo01.py::test_demo09
0.81s call test_demo01.py::test_demo08
0.71s call test_demo01.py::test_demo07
0.60s call test_demo01.py::test_demo06
========================================================================== 11 passed in 5.61s ==========================================================================
二、显示所有用例的执行时间
(1)使用pytest --durations=0 即可显示所有用例的执行时间
$ pytest --durations=0
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items
test_demo01.py ........... [100%]
========================================================================== slowest durations ===========================================================================
1.01s call test_demo01.py::test_demo10
0.92s call test_demo01.py::test_demo09
0.80s call test_demo01.py::test_demo08
0.71s call test_demo01.py::test_demo07
0.60s call test_demo01.py::test_demo06
0.51s call test_demo01.py::test_demo05
0.40s call test_demo01.py::test_demo04
0.31s call test_demo01.py::test_demo03
0.20s call test_demo01.py::test_demo02
0.12s call test_demo01.py::test_demo01
(23 durations < 0.005s hidden. Use -vv to show these durations.)
========================================================================== 11 passed in 5.63s ==========================================================================
(2)细心的发现上面并没有显示所有的,test_demo11用例没有显示,这是因为pytest默认显示的耗时大于0.005秒的用例,小于0.005秒的用例默认是不显示的,如果要显示耗时小于0.005秒的用例,需要加上 -vv的参数
$ pytest --durations=0 -vv
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items
test_demo01.py::test_demo01 PASSED [ 9%]
test_demo01.py::test_demo02 PASSED [ 18%]
test_demo01.py::test_demo03 PASSED [ 27%]
test_demo01.py::test_demo04 PASSED [ 36%]
test_demo01.py::test_demo05 PASSED [ 45%]
test_demo01.py::test_demo06 PASSED [ 54%]
test_demo01.py::test_demo07 PASSED [ 63%]
test_demo01.py::test_demo08 PASSED [ 72%]
test_demo01.py::test_demo09 PASSED [ 81%]
test_demo01.py::test_demo10 PASSED [ 90%]
test_demo01.py::test_demo11 PASSED [100%]
========================================================================== slowest durations ===========================================================================
1.01s call test_demo01.py::test_demo10
0.91s call test_demo01.py::test_demo09
0.81s call test_demo01.py::test_demo08
0.71s call test_demo01.py::test_demo07
0.60s call test_demo01.py::test_demo06
0.51s call test_demo01.py::test_demo05
0.40s call test_demo01.py::test_demo04
0.30s call test_demo01.py::test_demo03
0.21s call test_demo01.py::test_demo02
0.11s call test_demo01.py::test_demo01
0.00s setup test_demo01.py::test_demo01
0.00s setup test_demo01.py::test_demo06
0.00s setup test_demo01.py::test_demo08
0.00s setup test_demo01.py::test_demo10
0.00s teardown test_demo01.py::test_demo01
0.00s teardown test_demo01.py::test_demo11
0.00s setup test_demo01.py::test_demo04
0.00s teardown test_demo01.py::test_demo02
0.00s teardown test_demo01.py::test_demo07
0.00s teardown test_demo01.py::test_demo10
0.00s teardown test_demo01.py::test_demo03
0.00s teardown test_demo01.py::test_demo04
0.00s teardown test_demo01.py::test_demo08
0.00s teardown test_demo01.py::test_demo06
0.00s setup test_demo01.py::test_demo09
0.00s setup test_demo01.py::test_demo02
0.00s teardown test_demo01.py::test_demo09
0.00s setup test_demo01.py::test_demo05
0.00s setup test_demo01.py::test_demo03
0.00s setup test_demo01.py::test_demo07
0.00s setup test_demo01.py::test_demo11
0.00s teardown test_demo01.py::test_demo05
0.00s call test_demo01.py::test_demo11
========================================================================== 11 passed in 5.63s ==========================================================================
三、显示所有耗时大于t秒的用例
(1)使用 pytest --durations=0 --durations-min=0.5 即可显示所有耗时大于0.5秒的用例
$ pytest --durations=0 --durations-min=0.5
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items
test_demo01.py ........... [100%]
========================================================================== slowest durations ===========================================================================
1.01s call test_demo01.py::test_demo10
0.91s call test_demo01.py::test_demo09
0.81s call test_demo01.py::test_demo08
0.71s call test_demo01.py::test_demo07
0.61s call test_demo01.py::test_demo06
0.51s call test_demo01.py::test_demo05
(27 durations < 0.5s hidden. Use -vv to show these durations.)
========================================================================== 11 passed in 5.63s ==========================================================================