Collection tree objects โ
These are the collector and item classes (collectively called โnodesโ) which make up the collection tree.
Node โ
classNodeBase class of
CollectorandItem, the components of the test collection tree.Collector's are the internal nodes of the tree, andItem's are the leaf nodes.fspath:
LocalPathA
LEGACY_PATHcopy of the path attribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
classmethodfrom_parent(parent, **kw)Public constructor for Nodes.
This indirection got introduced in order to enable removing the fragile logic from the node constructors.
Subclasses can use
super().from_parent(...)when overriding the construction.Parameters:
parent (Node)โ The parent node of this Node.
propertyihook- fspath-sensitive hook proxy used to call pytest hooks.
warn(
warning)Issue a warning for this Node.
Warnings will be displayed after the test session, unless explicitly suppressed.
Parameters:
warning (Warning)โ The warning instance to issue.
Raises:
ValueErrorโ Ifwarninginstance is not a subclass of Warning.
Example usage:
pythonnode.warn(PytestWarning("some message")) node.warn(UserWarning("some message"))node.warn(PytestWarning("some message")) node.warn(UserWarning("some message"))Changed in version 6.2: Any subclass of
Warningis now accepted, rather than onlyPytestWarningsubclasses.propertynodeid:strA ::-separated string denoting its collection tree address.listchain()
Return list of all parent collectors up to self, starting from the root of collection tree.
Returns: The nodes.
Return type:
List[Node]add_marker(
marker, append=True)Dynamically add a marker object to the node.
Parameters:
marker (Union[str, MarkDecorator])โ The marker.append (bool)โ Whether to append the marker, or prepend it.
iter_markers(
name=None)Iterate over all markers of the node.
Parameters:
name (Optional[str])โ If given, filter the results by the name attribute.
Returns: An iterator of the markers of the node.
Return type:
Iterator[Mark]for ... initer_markers_with_node(name=None)Iterate over all markers of the node.
Parameters:
name (Optional[str])โ If given, filter the results by the name attribute.
Returns: An iterator of (node, mark) tuples.
Return type:
Iterator[Tuple[Node, Mark]]get_closest_marker(
name: str) โOptional[Mark]get_closest_marker(
name: str, default: Mark) โMarkReturn the first marker matching the name, from closest (for example function) to farther level (for example module level).
Parameters:
defaultโ Fallback return value if no marker was found.nameโ Name to filter by.
listextrakeywords()
Return a set of all extra keywords in self and any parents.
addfinalizer(
fin)Register a function to be called without arguments 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().getparent(
cls)Get the next parent node (including self) which is an instance of the given class.
Parameters:
cls (Type[_NodeType])โ The node class to search for.
Returns: The node, if found.
Return type:
Optional[_NodeType]repr_failure(
excinfo, style=None)Return a representation of a collection or test failure.
See also
Parameters:
excinfo (ExceptionInfo[BaseException])โ Exception information for the failure.
Collector โ
classCollector- Bases:
Node
Base class of all collectors.
Collectorcreate children throughcollect()and thus iteratively build the collection tree.exceptionCollectErrorBases:
ExceptionAn error during collection, contains a custom message.
collect()
Collect children (items and collectors) for this collector.
repr_failure(
excinfo)Return a representation of a collection failure.
Parameters:
excinfo (ExceptionInfo[BaseException])โ Exception information for the failure.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be
None).fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
- Bases:
Item โ
classItem- Bases:
Node
Base class of all test invocation items.
Note that for a single function there might be multiple test invocation items.
user_properties:
List[Tuple[str, object]]A list of tuples (name, value) that holds user defined properties for this test.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
runtest()
Run the test case for this item.
Must be implemented by subclasses.
See also
add_report_section(
when, key, content)Add a new report section, similar to whatโs done internally to add stdout and stderr captured output:
pythonitem.add_report_section("call", "stdout", "report section contents")item.add_report_section("call", "stdout", "report section contents")Parameters:
when (str)โ One of the possible capture states,"setup","call","teardown".key (str)โ Name of the section, can be customized at will. Pytest uses"stdout"and"stderr"internally.content (str)โ The full contents as a string.
reportinfo()
Get location information for this item for test reports.
Returns a tuple with three elements:
The path of the test (default
self.path)The 0-based line number of the test (default
None)A name of the test to be shown (default
"")
See also
propertylocation:Tuple[str, Optional[int], str]Returns a tuple of
(relfspath, lineno, testname)for this item whererelfspathis file path relative toconfig.rootpathand lineno is a 0-based line number.
- Bases:
File โ
classFile- Bases:
FSCollector
Base class for collecting tests from a file.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
fspath:
LocalPathA
LEGACY_PATHcopy of the path attribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
- Bases:
FSCollector โ
classFSCollector- Bases:
Collector
Base class for filesystem collectors.
path:
PathFilesystem path where this node was collected from (can be
None).classmethodfrom_parent(parent, *, fspath=None, path=None, **kw)The public constructor.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
fspath:
LocalPathA
LEGACY_PATHcopy of the path attribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
- Bases:
Session โ
final classSession- Bases:
FSCollector
The root of the collection tree.
Sessioncollects the initial paths given as arguments to pytest.exceptionInterrupted- Bases:
KeyboardInterrupt
Signals that the test run was interrupted.
- Bases:
exceptionFailed- Bases:
Exception
Signals a stop as failed test run.
- Bases:
propertystartpath:PathThe path from which pytest was invoked.
New in version 7.0.0.
perform_collect(
args: Optional[Sequence[str]] = None, genitems: Literal[True] = True) โSequence[Item]perform_collect(
args: Optional[Sequence[str]] = None, genitems: bool = True) โSequence[Union[Item, Collector]]Perform the collection phase for this session.
This is called by the default
pytest_collectionhook implementation; see the documentation of this hook for more details. For testing purposes, it may also be called directly on a freshSession.This function normally recursively expands any collectors collected from the session to their items, and only items are returned. For testing purposes, this may be suppressed by passing
genitems=False, in which case the return value contains these collectors unexpanded, andsession.itemsis empty.for ... incollect()Collect children (items and collectors) for this collector.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be
None).
- Bases:
Package โ
classPackage- Bases:
Module
Collector for files and directories in a Python packages โ directories with an
__init__.pyfile.name:
strA unique name within the scope of the parent node.
for ... incollect()Collect children (items and collectors) for this collector.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
- Bases:
Module โ
classModule- Bases:
File,PyCollector
Collector for test classes and functions in a Python module.
collect()
Collect children (items and collectors) for this collector.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
- Bases:
Class โ
classClass- Bases:
PyCollector
Collector for test methods (and nested classes) in a Python class.
classmethodfrom_parent(parent, *, name, obj=None, **kw)The public constructor.
collect()
Collect children (items and collectors) for this collector.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
- Bases:
Function โ
classFunction- Bases:
PyobjMixin,Item
Item responsible for setting up and executing a Python test function.
Parameters:
name โ The full function name, including any decorations like those added by parametrization (
my_func[my_param]).parent โ The parent Node.
config โ The pytest Config object.
callspec โ If given, this is function has been parametrized and the callspec contains meta information about the parametrization.
callobj โ If given, the object which will be called when the Function is invoked, otherwise the callobj will be obtained from
parentusingoriginalname.keywords โ Keywords bound to the function object for โ-kโ matching.
session โ The pytest Session object.
fixtureinfo โ Fixture information already resolved at this fixture node..
originalname โ The attribute name to use for accessing the underlying function object. Defaults to
name. Set this if name is different from the original name, for example when it contains decorations like those added by parametrization (my_func[my_param]).
originalname
Original function name, without any decorations (for example parametrization adds a
"[...]"suffix to function names), used to access the underlying function object fromparent(in casecallobjis not given explicitly).New in version 3.0.
classmethodfrom_parent(parent, **kw)The public constructor.
propertyfunctionUnderlying python โfunctionโ object.
runtest()
Execute the underlying test function.
repr_failure(
excinfo)Return a representation of a collection or test failure.
See also
Parameters:
excinfo (ExceptionInfo[BaseException])โ Exception information for the failure.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
user_properties:
List[Tuple[str, object]]A list of tuples (name, value) that holds user defined properties for this test.
- Bases:
FunctionDefinition โ
classFunctionDefinition- Bases:
Function
This class is a stop gap solution until we evolve to have actual function definition nodes and manage to get rid of
metafunc.runtest()
Execute the underlying test function.
name:
strA unique name within the scope of the parent node.
parent
The parent collector node.
config:
ConfigThe pytest config object.
session:
SessionThe pytest session this node is part of.
path:
PathFilesystem path where this node was collected from (can be None).
setup()
Execute the underlying test function.
fspath:
LocalPathA
LEGACY_PATHcopy of thepathattribute. Intended for usage for methods not migrated topathlib.Pathyet, such asItem.reportinfo(). Will be deprecated in a future release, prefer usingpathinstead.keywords:
MutableMapping[str, Any]Keywords/markers collected from all scopes.
own_markers:
List[Mark]The marker objects belonging to this node.
extra_keyword_matches:
Set[str]Allow adding of extra keywords to use for matching.
stash:
StashA place where plugins can store information on the node for their own use.
user_properties:
List[Tuple[str, object]]A list of tuples (name, value) that holds user defined properties for this test.
- Bases: