Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/pytest_describe/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ def from_parent(cls, parent, obj):
self.name = name
self.funcobj = obj
return self

@property
def describe_function_heirarchy(self):
"""Get the hierarchy of describe functions leading to and including this block"""
if not hasattr(self, "_describe_function_heirarchy"):
hierarchy = []
current = self

while current:
if isinstance(current, DescribeBlock):
hierarchy.append(current.funcobj)
current = current.parent

self._describe_function_heirarchy = hierarchy
return self._describe_function_heirarchy

def collect(self):
"""Get list of children"""
Expand Down Expand Up @@ -136,7 +151,19 @@ def pytest_pycollect_makeitem(collector, name, obj):
for prefix in collector.config.getini("describe_prefixes"):
if obj.__name__.startswith(prefix):
return DescribeBlock.from_parent(collector, obj)


def pytest_collection_modifyitems(session, config, items):
"""Add API to all test items to get the describe heirarchy."""
for item in items:
item.get_describe_function_heirarchy = types.MethodType(get_describe_function_heirarchy, item)

def get_describe_function_heirarchy(self):
"""Get the hierarchy of describe functions leading to this test item"""
return (
self.parent.describe_function_heirarchy
if hasattr(self, "parent") and hasattr(self.parent, "describe_function_heirarchy")
else []
)

def pytest_addoption(parser):
"""Add configuration option describe_prefixes."""
Expand Down