09、自动执行

除了使用pytest.fixture.userfixtures和传参两种显式的调用fixture之外.也可以自动的执行fixture,在定义fixture的时候设置autouser=True即可

from datetime import datetime
from time import sleep

import pytest


@pytest.fixture(autouse=True)
def timer_session_scope():
    start = datetime.now()
    print(f"\nstart:{start}")
    yield
    end = datetime.now()
    print(f"total time cost:{end - start}")


def test_timer():
    sleep(1.3)
    assert 2 == 2

运行结果:

╰ pytest -v -s test_data.py
======================================================= test session starts ========================================================
platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}}
rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini
plugins: bdd-3.1.0, html-1.20.0, metadata-1.8.0
collected 1 item                                                                                                                   

test_data.py::test_timer 
start:2019-09-27 11:12:13.526151
PASSEDtotal time cost:0:00:01.305416


===================================================== 1 passed in 1.33 seconds =====================================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值