美文网首页
Pytest fixture接收参数(demo)

Pytest fixture接收参数(demo)

作者: 风中骄子 | 来源:发表于2018-07-19 14:33 被阅读0次

    定义作用于为function的fixture

    import pytest
    import time
    
    @pytest.fixture(scope="function")
    def func_header(request):
        print('\n-----------------')
        print('function    : %s' % request.function.__name__)
        print('time        : %s' % time.asctime())
        print('-----------------')
        return "fun"
    
    @pytest.fixture(scope="module")
    def mod_header(request):
        print('\n-----------------')
        print('module      : %s' % request.module.__name__)
        print('-----------------')
        return "mod"
    

    case里接受参数

    class TestDemo(object):
        def test_one(self, mod_header, func_header):
            print('in test_one()')
            print(mod_header)
            print(func_header)
    
        def test_two(self, mod_header, func_header):
            print('in test_two()')
            print(mod_header)
            print(func_header)
    

    执行效果

    (csmp) [root@bogon pytest_case]# pytest -v -s test_demo4.py 
    =========================================================================== test session starts ============================================================================
    platform linux -- Python 3.6.4, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 -- /root/anaconda3/envs/csmp/bin/python
    cachedir: .pytest_cache
    metadata: {'Python': '3.6.4', 'Platform': 'Linux-3.10.0-514.21.1.el7.x86_64-x86_64-with-centos-7.3.1611-Core', 'Packages': {'pytest': '3.5.1', 'py': '1.5.3', 'pluggy': '0.6.0'}, 'Plugins': {'xdist': '1.22.2', 'rerunfailures': '4.0', 'metadata': '1.7.0', 'html': '1.17.0', 'forked': '0.2', 'allure-adaptor': '1.7.10'}}
    rootdir: /opt/chenfei/csmp/automation/csmp/pytest_case, inifile:
    plugins: xdist-1.22.2, rerunfailures-4.0, metadata-1.7.0, html-1.17.0, forked-0.2, allure-adaptor-1.7.10
    collected 2 items                                                                                                                                                          
    
    test_demo4.py::TestDemo::test_one 
    -----------------
    module      : automation.csmp.pytest_case.test_demo4
    -----------------
    
    -----------------
    function    : test_one
    time        : Thu Jul 19 14:32:05 2018
    -----------------
    in test_one()
    mod
    fun
    PASSED
    test_demo4.py::TestDemo::test_two 
    -----------------
    function    : test_two
    time        : Thu Jul 19 14:32:05 2018
    -----------------
    in test_two()
    mod
    fun
    PASSED
    
    ========================================================================= 2 passed in 0.02 seconds =========================================================================
    

    相关文章

      网友评论

          本文标题:Pytest fixture接收参数(demo)

          本文链接:https://www.haomeiwen.com/subject/jrltmftx.html