简体   繁体   English

有没有办法从简单(非测试)函数直接引用pytest fixture?

[英]Is there way to directly reference to a pytest fixture from a simple (non test) function?

Is there a way to reference (and call into) a pytest fixture from a simple function that itself is not either a test_* function or also a fixture? 有没有办法从一个简单的函数引用(和调用)pytest fixture,它本身不是test_ *函数或者也不是fixture?

known examples that can use fixtures: 可以使用灯具的已知示例:

1) 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

I want to be able to do this: 3) 我希望能够做到这一点:3)

def my_simple_function( the, rest of, my, args):
    blah
    blah
    outright.reference.this.pytest.fixture.some_cool_pytest_fixture_name(args)
    blah

NOTE: 注意:

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

^^^ This in on a system which does have the record_xml_property ^^^这个在具有record_xml_property的系统上

My desire is to be able to do something like this: 我的愿望是能够做到这样的事情:

try: 
     from pytest import record_xml_property as property_handler 
 except: 
     @pytest.fixture() 
     def property_handler(mykey, myval): 
         print('{0}={1}'.format(mykey,myval)

^^^ If the above can succeed, then I can always depend on property_handler being there for me as a fixture. ^^^如果以上可以成功,那么我总是可以依赖property_handler作为一个夹具。

When you use pytest's built in fixture handling, you don't have to worry about how to manage it's availability and memory. 当您使用pytest的内置夹具处理时,您不必担心如何管理它的可用性和内存。

So when you aren't using pytest, you can just import it as a normal function. 因此,当您不使用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)


Edit 编辑

In response to Ken's note, I spent some time trying to access the list of available fixtures defined in pytest. 为了回应Ken的说明,我花了一些时间试图访问pytest中定义的可用夹具列表。 I've come up with two ways to access the list, but haven't pushed it far enough to get the list in a python list format, only the console output. 我已经提出了两种方法来访问列表,但是还没有把它推得足够远以获得python列表格式的列表,只有控制台输出。

From the command line, you can run pytest --fixtures to list all fixtures available. 从命令行,您可以运行pytest --fixtures来列出所有可用的灯具。 To do the same thing from a python script, you can run this code 要从python脚本执行相同的操作,您可以运行此代码

 import pytest from _pytest import python from _pytest import config configs = config._prepareconfig() python.showfixtures(configs) 

I think you could access the list if you dig into the pytest Session object and look into its _fixturemanager attribute, but I couldn't figure out a way to create these like the function showfixtures above does. 如果你深入了解pytest Session对象并查看它的_fixturemanager属性,我认为你可以访问列表,但我无法找到一种方法来创建这些像上面的函数showfixtures一样。

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

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