其他分享
首页 > 其他分享> > pytest-setup与用例之间传参

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

 

标签:传参,test1,begin,setup,param,用例,case,time,print
来源: https://www.cnblogs.com/amber10086/p/16560178.html