pytest setup与用例之间传参

pytest前置钩子(setup)与用例之间互相传参

  1. 方法一:
import pytest
import time

@pytest.fixture()
def setup(request):
    begin_time = time.time()
    print('param from case: %s' % request.param)
    return begin_time

@pytest.mark.parametrize('setup',[1,5], indirect=True)  #indirect 把setup当作函数执行
@pytest.mark.parametrize('a,b',
                         [(1,5),(3,4)])
def test_param(setup,a,b):
    begin_time = setup
    print('begin_time is: ', begin_time)
    print('param_a', a)
    print('param_b', b)

执行结果:

Launching pytest with arguments test1.py::test_param --no-header --no-summary -q in D:\pythontest

============================= test session starts =============================
collecting ... collected 4 items

test1.py::test_param[1-5-1] param from case: 1
PASSED                                       [ 25%]begin_time is:  1659103133.1927586
param_a 1
param_b 5

test1.py::test_param[1-5-5] param from case: 5
PASSED                                       [ 50%]begin_time is:  1659103133.1967208
param_a 1
param_b 5

test1.py::test_param[3-4-1] param from case: 1
PASSED                                       [ 75%]begin_time is:  1659103133.2007089
param_a 3
param_b 4

test1.py::test_param[3-4-5] param from case: 5
PASSED                                       [100%]begin_time is:  1659103133.2037005
param_a 3
param_b 4


============================== 4 passed in 0.02s ==============================

Process finished with exit code 0

方法二:发现了cache的使用,进一步优化(如果是多个用例文件共用setup,则可以把setup添加至conftest)

import pytest
import time

@pytest.fixture()
def setup(cache, request):
    begin_time = time.time()
    print('param from case: %s' % request.param)
    cache.set('begin_time', begin_time)
    # return begin_time
    yield
    print('after case')
    print(cache.get('a',default='haha'))
    print(cache.get('b',default='haha'))
    print(cache.get('my_dict',default='haha'))


@pytest.mark.parametrize('setup',[1,5], indirect=True)
@pytest.mark.parametrize('a,b',
                         [(1,5),(3,4)])
def test1_param(cache,setup,a,b):
    begin_time = cache.get('begin_time','haha')
    print('begin_time is: ', begin_time)
    print('param_a', a)
    print('param_b', b)
    cache.set('a', a)
    cache.set('b', b)
    cache.set('my_dict', {1:2, 2:'heihei'})


if __name__ == '__main__':
     pytest.main(['-sv', 'test1.py'])

执行结果:

============================= test session starts =============================
collecting ... collected 4 items

test1.py::test1_param[1-5-1] param from case: 1
PASSED                                      [ 25%]begin_time is:  1659190713.9223804
param_a 1
param_b 5
after case
1
5
{'1': 2, '2': 'heihei'}

test1.py::test1_param[1-5-5] param from case: 5
PASSED                                      [ 50%]begin_time is:  1659190713.9303615
param_a 1
param_b 5
after case
1
5
{'1': 2, '2': 'heihei'}

test1.py::test1_param[3-4-1] param from case: 1
PASSED                                      [ 75%]begin_time is:  1659190713.9403338
param_a 3
param_b 4
after case
3
4
{'1': 2, '2': 'heihei'}

test1.py::test1_param[3-4-5] param from case: 5
PASSED                                      [100%]begin_time is:  1659190713.9483128
param_a 3
param_b 4
after case
3
4
{'1': 2, '2': 'heihei'}


============================== 4 passed in 0.04s ==============================

Process finished with exit code 0
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值