gpt4 book ai didi

python - 有没有办法从简单(非测试)函数直接引用 pytest 固定装置?

转载 作者:行者123 更新时间:2023-12-01 04:47:57 25 4
gpt4 key购买 nike

有没有办法从一个简单的函数中引用(并调用)一个 pytest 固定装置,它本身既不是 test_* 函数也不是固定装置?

可以使用 fixture 的已知示例:

1)

def test_mytest( some_cool_pytest_fixture_name, the, rest of, my, args):
blah
blah
some_cool_pytest_fixture_name(args)
blah

2)
@pytest.fixture()
def my_new_fixture( some_cool_pytest_fixture_name, the, rest of, my, args):
blah
blah
some_cool_pytest_fixture_name(args)
blah

我希望能够做到这一点:
3)
def my_simple_function( the, rest of, my, args):
blah
blah
outright.reference.this.pytest.fixture.some_cool_pytest_fixture_name(args)
blah

注意:
from pytest import record_xml_property as property_handler
** E ImportError: cannot import name record_xml_property**

^^^ 这在具有 的系统上record_xml_property

我的愿望是能够做这样的事情:
try: 
from pytest import record_xml_property as property_handler
except:
@pytest.fixture()
def property_handler(mykey, myval):
print('{0}={1}'.format(mykey,myval)

^^^ 如果以上都能成功,那我就可以一直靠 property_handler 作为固定装置在我身边。

最佳答案

当您使用 pytest 的内置 fixture 处理时,您不必担心如何管理它的可用性和内存。

因此,当您不使用 pytest 时,您可以将其作为普通函数导入。

from path.to.cool.pytest.fixture import some_cool_pytest_fixture_name

def my_simple_function(the, rest of, my, args):
some_cool_pytest_fixture_name(args)

编辑

为了响应 Ken 的说明,我花了一些时间尝试访问 pytest 中定义的可用设备列表。我想出了两种访问列表的方法,但还没有把它推得足够远,无法以 python 列表格式获取列表,只有控制台输出。

从命令行,您可以运行 pytest --fixtures列出所有可用的灯具。要从 python 脚本执行相同的操作,您可以运行此代码
import pytest
from _pytest import python
from _pytest import config
configs = config._prepareconfig()
python.showfixtures(configs)

我认为如果您深入研究 pytest Session 对象并查看其 _fixturemanager,您可以访问该列表。属性,但我想不出创建这些函数的方法 showfixtures上面没有。

关于python - 有没有办法从简单(非测试)函数直接引用 pytest 固定装置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com