pytest中自动计算每个用例执行的时间,参考官网

今天看pytest官网无意间发现了这个好用的方法.无需修改自己的用例,自动计算

官网地址

conftest.py

import pytest
import time
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'


@pytest.fixture(scope='session', autouse=True)
def timer_session_scope():
    start = time.time()
    print('\nstart: {}'.format(time.strftime(DATE_FORMAT, time.localtime(start))))

    yield

    finished = time.time()
    print('finished: {}'.format(time.strftime(DATE_FORMAT, time.localtime(finished))))
    print('Total time cost: {:.3f}s'.format(finished - start))


@pytest.fixture(autouse=True)
def timer_function_scope():
    start = time.time()
    yield
    print(' Time cost: {:.3f}s'.format(time.time() - start))
posted @ 2022-07-01 11:29  Tarzen  阅读(609)  评论(0编辑  收藏  举报