Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find what fixtures a test uses?

I want to automatically mark tests based on which fixtures they use. For instance, if a test uses a fixture named spark, I'd like to add a marker called uses_spark so that I can automatically ignore them.

I know I can use pytest_collection_modifyitems in conftest.py to add markers.

def pytest_collection_modifyitems(items):
  for item in items:
    if uses_spark_fixture(item):
      item.add_marker(pytest.mark.spark)

def uses_spark_fixture(item):
  ???

How do I implement uses_spark_fixture?

like image 369
kelloti Avatar asked Oct 16 '22 11:10

kelloti


1 Answers

Each item stores the list of used fixtures in the fixturenames attribute. the check is thus quite simple:

def pytest_collection_modifyitems(items):
    for item in items:
        if 'spark_fixture' in item.fixturenames:
            item.add_marker(pytest.mark.spark)
like image 163
hoefling Avatar answered Oct 20 '22 23:10

hoefling