• 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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    执行结果:

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    方法二:发现了cache的使用,进一步优化

    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'])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    执行结果:

    ============================= 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    爬虫学习(05): 数据解析_bs4篇
    uni-app学习二
    以工程化路径破题,中国系统推动数据要素化
    SARAS算法
    基于php企业办公文件分类系统flask-django-python-nodejs
    字节首席架构师整合面试痛点,成就399页Java框架核心宝典
    邬贺铨:因地制宜 数字化技术赋能“双碳”实践
    【洛谷】P1242 新汉诺塔
    QSPI介绍
    比亚迪越来越像华为?
  • 原文地址:https://blog.csdn.net/DaxiaLeeSuper/article/details/126064720