繁体   English   中英

pytest 获取运行时函数测试变量

[英]pytest get runtime function test variable

我正在使用插件重复并且需要在运行时提取当前测试(用于记录器)。 我假设我将访问:dummy.py::test_dummy[1-4]

@pytest.fixture(scope="session")
def initial_setup():
    print(f'\n\n<<< setup >>>')
    counter_not_working = 0
    yield counter_not_working
    print('\n<<< teardown >>>')

@pytest.mark.repeat(int(os.getenv("repeat", "4")))
def test_dummy(initial_setup):

    # Alt.1
    counter_not_working = initial_setup
    counter_not_working = + 1
    print(f'\nTo be used for a logger..., test no: {counter_not_working}???')

    # Alt.2
    counter_get_from_pytest = 0  # ???
    print(f'To be used for a logger..., test no: {counter_get_from_pytest}???')
    pass

============================ 测试会话开始 ================== ============ linux 平台 -- Python 3.7.5, pytest-4.4.1, py-1.8.0, pluggy-0.9.0 -- xxx/xxx cachedir: .pytest_cache rootdir: / xxx/, inifile: pytest.ini plugins: repeat-0.8.0 收集...收集 4 项

dummy.py::test_dummy[1-4]

<<< setup >>> PASSED [ 25%] 用于记录器...,测试编号:1??? 用于记录器......,测试编号:0???

dummy.py::test_dummy[2-4] PASSED [ 50%] 用于记录器......,测试编号:1??? 用于记录器......,测试编号:0???

dummy.py::test_dummy[3-4] PASSED [ 75%] 用于记录器......,测试编号:1??? 用于记录器......,测试编号:0???

dummy.py::test_dummy[4-4] PASSED [100%] 用于记录器......,测试编号:1??? 用于记录器......,测试编号:0???

<<<拆解>>>

============================ 4 在 0.02 秒内通过 ================== ==========

进程以退出代码 0 结束

我发现我自己:

def test_dummy(initial_setup, request ): no_of_tests = str( request.node ) print(type(no_of_tests))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM