How can I find what fixtures a test uses?

Viewed 248

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?

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