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?
known examples that can use fixtures:
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)
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
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.
When you use pytest's built in fixture handling, you don't have to worry about how to manage it's availability and memory.
So when you aren't using pytest, you can just import it as a normal function.
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)
In response to Ken's note, I spent some time trying to access the list of available fixtures defined in 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.
From the command line, you can run pytest --fixtures
to list all fixtures available. To do the same thing from a python script, you can run this code
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With