Skip to content

item_attribute_accessor

This module contains the ItemAttributeAccessor class which is used to access various attributes of a pytest item.

It includes methods and properties to get the scenario, check if the item is a scenario, get the tags, get the node ID, get the name, and get all properties.

ItemAttributeAccessor

A class that accesses various attributes of a pytest item.

Attributes:

Name Type Description
item Item

The pytest item object.

properties_names set

A set of property names.

Properties

scenario: Returns the scenario attribute of the item. is_scenario: Returns a boolean indicating if the item is a scenario. tags: Returns a list of tags of the scenario. node_id: Returns the node ID of the item. name: Returns the name of the item.

Methods:

Name Description
__init__

Initializes the ItemAttributeAccessor class.

get_properties

Returns a dictionary of all properties.

Source code in libs\cafex_core\src\cafex_core\utils\item_attribute_accessor.py
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class ItemAttributeAccessor:
    """A class that accesses various attributes of a pytest item.

    Attributes:
        item (Item): The pytest item object.
        properties_names (set): A set of property names.

    Properties:
        scenario: Returns the scenario attribute of the item.
        is_scenario: Returns a boolean indicating if the item is a scenario.
        tags: Returns a list of tags of the scenario.
        node_id: Returns the node ID of the item.
        name: Returns the name of the item.

    Methods:
        __init__: Initializes the ItemAttributeAccessor class.
        get_properties: Returns a dictionary of all properties.
    """

    def __init__(self, item):
        """Initialize the ItemAttributeAccessor class.

        Args:
            item: The pytest item object.
        """
        self.item = item
        self.properties_names = {"name", "node_id", "tags", "test_type"}

    @property
    def scenario(self):
        """Returns the scenario attribute of the item.

        Returns:
            Scenario: The scenario attribute of the item.
        """
        return getattr(self.item.obj, "__scenario__", None)

    @property
    def is_scenario(self):
        """Returns a boolean indicating if the item is a scenario.

        Returns:
            bool: True if the item is a scenario, False otherwise.
        """
        return bool(getattr(self.item.obj, "__scenario__", False))

    @property
    def tags(self):
        """Returns a list of tags of the scenario."""
        if self.is_scenario:
            # For pytest-bdd scenarios
            return list(getattr(self.scenario, "tags", set()))
        return [marker.name for marker in self.item.iter_markers()]

    @property
    def node_id(self):
        """Returns the node ID of the item.

        Returns:
            str: The node ID of the item.
        """
        return self.item.nodeid

    @property
    def name(self):
        """Returns the name of the item.

        Returns:
            str: The name of the item.
        """
        return self.item.name

    @property
    def test_type(self):
        """Returns the type of the test."""
        if self.is_scenario:
            return "pytestBdd"
        if (
            hasattr(self.item, "cls")
            and self.item.cls is not None
            and isinstance(self.item.cls, type)
            and issubclass(self.item.cls, unittest.TestCase)
        ):
            return "unittest"
        return "pytest"

    def get_properties(self):
        """Returns a dictionary of all properties.

        Returns:
            dict: A dictionary of all properties.
        """
        properties = {
            "name": self.name,
            "nodeId": self.node_id,
            "tags": self.tags,
            "testType": self.test_type,
        }
        return properties

    @property
    def extended_properties(self):
        """Returns additional test metadata."""
        props = self.get_properties()

        # Add location info
        props.update(
            {
                "location": {
                    "file": self.item.location[0],
                    "line": self.item.location[1],
                    "module": self.item.location[2] if len(self.item.location) > 2 else None,
                }
            }
        )

        # Add parametrization info if available
        if hasattr(self.item, "callspec"):
            props["parameters"] = dict(self.item.callspec.params)

        # Add function docstring if available
        if self.item.function.__doc__:
            props["description"] = self.item.function.__doc__.strip()

        return props

extended_properties property

Returns additional test metadata.

is_scenario property

Returns a boolean indicating if the item is a scenario.

Returns:

Name Type Description
bool

True if the item is a scenario, False otherwise.

name property

Returns the name of the item.

Returns:

Name Type Description
str

The name of the item.

node_id property

Returns the node ID of the item.

Returns:

Name Type Description
str

The node ID of the item.

scenario property

Returns the scenario attribute of the item.

Returns:

Name Type Description
Scenario

The scenario attribute of the item.

tags property

Returns a list of tags of the scenario.

test_type property

Returns the type of the test.

__init__(item)

Initialize the ItemAttributeAccessor class.

Parameters:

Name Type Description Default
item

The pytest item object.

required
Source code in libs\cafex_core\src\cafex_core\utils\item_attribute_accessor.py
31
32
33
34
35
36
37
38
def __init__(self, item):
    """Initialize the ItemAttributeAccessor class.

    Args:
        item: The pytest item object.
    """
    self.item = item
    self.properties_names = {"name", "node_id", "tags", "test_type"}

get_properties()

Returns a dictionary of all properties.

Returns:

Name Type Description
dict

A dictionary of all properties.

Source code in libs\cafex_core\src\cafex_core\utils\item_attribute_accessor.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def get_properties(self):
    """Returns a dictionary of all properties.

    Returns:
        dict: A dictionary of all properties.
    """
    properties = {
        "name": self.name,
        "nodeId": self.node_id,
        "tags": self.tags,
        "testType": self.test_type,
    }
    return properties