def __init__(self, name, parent=None, config=None, session=None, fspath=None, nodeid=None): #: a unique name within the scope of the parent node self.name = name #: the parent collector node. self.parent = parent #: the pytest config object self.config = config or parent.config #: the session this node is part of self.session = session or parent.session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, 'fspath', None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} if nodeid is not None: self._nodeid = nodeid else: assert parent is not None self._nodeid = self.parent.nodeid + "::" + self.name
def __init__( self, name: str, parent: Optional["Node"] = None, config: Optional[Config] = None, session: Optional["Session"] = None, fspath: Optional[py.path.local] = None, nodeid: Optional[str] = None, ) -> None: #: a unique name within the scope of the parent node self.name = name #: the parent collector node. self.parent = parent #: the pytest config object if config: self.config = config else: if not parent: raise TypeError("config or parent must be provided") self.config = parent.config #: the session this node is part of if session: self.session = session else: if not parent: raise TypeError("session or parent must be provided") self.session = parent.session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, "fspath", None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: the marker objects belonging to this node self.own_markers = [] # type: List[Mark] #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # type: Set[str] # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} # type: Dict[str, FixtureDef] if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: if not self.parent: raise TypeError("nodeid or parent must be provided") self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name # A place where plugins can store information on the node for their # own use. Currently only intended for internal plugins. self._store = Store()
def __init__( self, name: str, parent: "Optional[Node]" = None, config: Optional[Config] = None, session: "Optional[Session]" = None, fspath: Optional[LEGACY_PATH] = None, path: Optional[Path] = None, nodeid: Optional[str] = None, ) -> None: #: A unique name within the scope of the parent node. self.name = name #: The parent collector node. self.parent = parent #: The pytest config object. if config: self.config: Config = config else: if not parent: raise TypeError("config or parent must be provided") self.config = parent.config #: The pytest session this node is part of. if session: self.session = session else: if not parent: raise TypeError("session or parent must be provided") self.session = parent.session #: Filesystem path where this node was collected from (can be None). self.path = _imply_path(path or getattr(parent, "path", None), fspath=fspath)[0] # The explicit annotation is to avoid publicly exposing NodeKeywords. #: Keywords/markers collected from all scopes. self.keywords: MutableMapping[str, Any] = NodeKeywords(self) #: The marker objects belonging to this node. self.own_markers: List[Mark] = [] #: Allow adding of extra keywords to use for matching. self.extra_keyword_matches: Set[str] = set() if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: if not self.parent: raise TypeError("nodeid or parent must be provided") self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name # A place where plugins can store information on the node for their # own use. Currently only intended for internal plugins. self._store = Store()
def __init__( self, name, parent=None, config=None, session: Optional["Session"] = None, fspath=None, nodeid=None, ) -> None: #: a unique name within the scope of the parent node self.name = name #: the parent collector node. self.parent = parent #: the pytest config object self.config = config or parent.config #: the session this node is part of if session is None: assert parent.session is not None self.session = parent.session else: self.session = session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, "fspath", None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: the marker objects belonging to this node self.own_markers = [] # type: List[Mark] #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # type: Set[str] # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} # type: Dict[str, FixtureDef] if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name
def __init__( self, name, parent=None, config=None, session=None, fspath=None, nodeid=None ): #: a unique name within the scope of the parent node # self.name = name self.name = name.encode("utf-8").decode("unicode_escape") #: the parent collector node. self.parent = parent #: the pytest config object self.config = config or parent.config #: the session this node is part of self.session = session or parent.session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, "fspath", None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: the marker objects belonging to this node self.own_markers = [] #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name
def __init__( self, name: str, parent: "Optional[Node]" = None, config: Optional[Config] = None, session: "Optional[Session]" = None, fspath: Optional[LEGACY_PATH] = None, path: Optional[Path] = None, nodeid: Optional[str] = None, ) -> None: #: A unique name within the scope of the parent node. self.name = name #: The parent collector node. self.parent = parent if config: #: The pytest config object. self.config: Config = config else: if not parent: raise TypeError("config or parent must be provided") self.config = parent.config if session: #: The pytest session this node is part of. self.session = session else: if not parent: raise TypeError("session or parent must be provided") self.session = parent.session if path is None and fspath is None: path = getattr(parent, "path", None) #: Filesystem path where this node was collected from (can be None). self.path: Path = _imply_path(type(self), path, fspath=fspath) # The explicit annotation is to avoid publicly exposing NodeKeywords. #: Keywords/markers collected from all scopes. self.keywords: MutableMapping[str, Any] = NodeKeywords(self) #: The marker objects belonging to this node. self.own_markers: List[Mark] = [] #: Allow adding of extra keywords to use for matching. self.extra_keyword_matches: Set[str] = set() if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: if not self.parent: raise TypeError("nodeid or parent must be provided") self._nodeid = self.parent.nodeid + "::" + self.name #: A place where plugins can store information on the node for their #: own use. #: #: :type: Stash self.stash = Stash() # Deprecated alias. Was never public. Can be removed in a few releases. self._store = self.stash
class Node(object): """ base class for Collector and Item the test collection tree. Collector subclasses have children, Items are terminal nodes.""" def __init__(self, name, parent=None, config=None, session=None, fspath=None, nodeid=None): #: a unique name within the scope of the parent node self.name = name #: the parent collector node. self.parent = parent #: the pytest config object self.config = config or parent.config #: the session this node is part of self.session = session or parent.session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, 'fspath', None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} if nodeid is not None: self._nodeid = nodeid else: assert parent is not None self._nodeid = self.parent.nodeid + "::" + self.name @property def ihook(self): """ fspath sensitive hook proxy used to call pytest hooks""" return self.session.gethookproxy(self.fspath) Module = _CompatProperty("Module") Class = _CompatProperty("Class") Instance = _CompatProperty("Instance") Function = _CompatProperty("Function") File = _CompatProperty("File") Item = _CompatProperty("Item") def _getcustomclass(self, name): maybe_compatprop = getattr(type(self), name) if isinstance(maybe_compatprop, _CompatProperty): return getattr(__import__('pytest'), name) else: cls = getattr(self, name) # TODO: reenable in the features branch # warnings.warn("use of node.%s is deprecated, " # "use pytest_pycollect_makeitem(...) to create custom " # "collection nodes" % name, category=DeprecationWarning) return cls def __repr__(self): return "<%s %r>" % (self.__class__.__name__, getattr(self, 'name', None)) def warn(self, code, message): """ generate a warning with the given code and message for this item. """ assert isinstance(code, str) fslocation = getattr(self, "location", None) if fslocation is None: fslocation = getattr(self, "fspath", None) self.ihook.pytest_logwarning.call_historic(kwargs=dict( code=code, message=message, nodeid=self.nodeid, fslocation=fslocation)) # methods for ordering nodes @property def nodeid(self): """ a ::-separated string denoting its collection tree address. """ return self._nodeid def __hash__(self): return hash(self.nodeid) def setup(self): pass def teardown(self): pass def listchain(self): """ return list of all parent collectors up to self, starting from root of collection tree. """ chain = [] item = self while item is not None: chain.append(item) item = item.parent chain.reverse() return chain def add_marker(self, marker): """ dynamically add a marker object to the node. ``marker`` can be a string or pytest.mark.* instance. """ from _pytest.mark import MarkDecorator, MARK_GEN if isinstance(marker, six.string_types): marker = getattr(MARK_GEN, marker) elif not isinstance(marker, MarkDecorator): raise ValueError("is not a string or pytest.mark.* Marker") self.keywords[marker.name] = marker def get_marker(self, name): """ get a marker object from this node or None if the node doesn't have a marker with that name. """ val = self.keywords.get(name, None) if val is not None: from _pytest.mark import MarkInfo, MarkDecorator if isinstance(val, (MarkDecorator, MarkInfo)): return val def listextrakeywords(self): """ Return a set of all extra keywords in self and any parents.""" extra_keywords = set() for item in self.listchain(): extra_keywords.update(item.extra_keyword_matches) return extra_keywords def listnames(self): return [x.name for x in self.listchain()] def addfinalizer(self, fin): """ register a function to be called when this node is finalized. This method can only be called when this node is active in a setup chain, for example during self.setup(). """ self.session._setupstate.addfinalizer(fin, self) def getparent(self, cls): """ get the next parent node (including ourself) which is an instance of the given class""" current = self while current and not isinstance(current, cls): current = current.parent return current def _prunetraceback(self, excinfo): pass def _repr_failure_py(self, excinfo, style=None): fm = self.session._fixturemanager if excinfo.errisinstance(fm.FixtureLookupError): return excinfo.value.formatrepr() tbfilter = True if self.config.option.fulltrace: style = "long" else: tb = _pytest._code.Traceback([excinfo.traceback[-1]]) self._prunetraceback(excinfo) if len(excinfo.traceback) == 0: excinfo.traceback = tb tbfilter = False # prunetraceback already does it if style == "auto": style = "long" # XXX should excinfo.getrepr record all data and toterminal() process it? if style is None: if self.config.option.tbstyle == "short": style = "short" else: style = "long" try: os.getcwd() abspath = False except OSError: abspath = True return excinfo.getrepr(funcargs=True, abspath=abspath, showlocals=self.config.option.showlocals, style=style, tbfilter=tbfilter) repr_failure = _repr_failure_py