Objects
Objects accessible from fixtures or hooks or importable from pytest.
CallInfo
final classCallInfo Result/Exception info of a function invocation.
excinfo:
Optional[ExceptionInfo[BaseException]]The captured exception of the call, if it raised.
start:
floatThe system time when the call started, in seconds since the epoch.
stop:
floatThe system time when the call ended, in seconds since the epoch.
duration:
floatThe call duration, in seconds.
when:
Literal['collect', 'setup', 'call', 'teardown']The context of invocation: “collect”, “setup”, “call” or “teardown”.
propertyresult:TResultThe return value of the call, if it didn’t raise.
Can only be accessed if excinfo is None.
classmethodfrom_call(func, when, reraise=None)Call func, wrapping the result in a CallInfo.
Parameters:
func (Callable[[], TResult])– The function to call. Called without arguments.when (Literal['collect', 'setup', 'call', 'teardown'])– The phase in which the function is called.reraise (Optional[Union[Type[BaseException], Tuple[Type[BaseException], ...]]])– Exception or - exceptions that shall propagate if raised by the function, instead of being wrapped in the CallInfo.
CollectReport
final classCollectReport - Bases:
BaseReport
Collection report object.
Reports can contain arbitrary extra attributes.
nodeid:
strNormalized collection nodeid.
outcome:
Literal['passed', 'failed', 'skipped']Test outcome, always one of “passed”, “failed”, “skipped”.
longrepr:
Union[None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr]None or a failure representation.
result
The collected items and collection nodes.
sections:
List[Tuple[str, str]]Tuples of str
(heading, content)with extra information for the test report. Used by pytest to add text captured fromstdout,stderr, and intercepted logging events. May be used by other plugins to add arbitrary information to reports.propertycaplog:strReturn captured log lines, if log capturing is enabled.
New in version 3.5.
propertycapstderr:strReturn captured text from stderr, if capturing is enabled.
New in version 3.0.
propertycapstdout:strReturn captured text from stdout, if capturing is enabled.
New in version 3.0.
propertycount_towards_summary:boolExperimental Whether this report should be counted towards the totals shown at the end of the test session: “1 passed, 1 failure, etc”.
Note
This function is considered experimental, so beware that it is subject to changes even in patch releases.
propertyfailed:boolWhether the outcome is failed.
propertyfspath:strThe path portion of the reported node, as a string.
propertyhead_line:Optional[str]Experimental The head line shown with longrepr output for this report, more commonly during traceback representation during failures:
shell________ Test.foo ________________ Test.foo ________In the example above, the head_line is “Test.foo”.
Note
This function is considered experimental, so beware that it is subject to changes even in patch releases.
propertylongreprtext:strRead-only property that returns the full string representation of
longrepr.New in version 3.0.
propertypassed:boolWhether the outcome is passed.
propertyskipped:boolWhether the outcome is skipped.
- Bases:
Config
final classConfig Access to configuration values, pluginmanager and plugin hooks.
Parameters:
pluginmanager (PytestPluginManager)– A pytestPluginManager.invocation_params (InvocationParams)– Object containing parameters regarding thepytest.main()invocation.
final classInvocationParams(*, args, plugins, dir)Holds parameters passed during
pytest.main().The object attributes are read-only.
New in version 5.1.
Note
Note that the environment variable
PYTEST_ADDOPTSand theaddoptsini option are handled by pytest, not being included in theargsattribute.Plugins accessing
InvocationParamsmust be aware of that.args:
Tuple[str, ...]The command-line arguments as passed to
pytest.main().plugins:
Optional[Sequence[Union[str, object]]]Extra plugins, might be
None.dir:
PathThe directory from which
pytest.main()was invoked.classArgsSource(value)Indicates the source of the test arguments.
New in version 7.2.
ARGS = 1
Command line arguments.
INCOVATION_DIR = 2
Invocation directory.
TESTPATHS = 3
‘testpaths’ configuration value.
option
Access to command line option as attributes.
- Type:
argparse.Namespace
- Type:
invocation_params
The parameters with which pytest was invoked.
Type:
InvocationParamspluginmanager
The plugin manager handles plugin registration and hook invocation.
Type:
PytestPluginManagerstash
A place where plugins can store information on the config for their own use.
Type:
Stashpropertyrootpath:PathThe path to the
rootdir.Type:
pathlib.PathNew in version 6.1.
propertyinipath:Optional[Path]The path to the configfile.
Type:
Optional[pathlib.Path]New in version 6.1.
add_cleanup(
func)Add a function to be called when the config object gets out of use (usually coinciding with
pytest_unconfigure).classmethodfromdictargs(option_dict, args)Constructor usable for subprocesses.
issue_config_time_warning(
warning, stacklevel)Issue and handle a warning during the “configure” stage.
During
pytest_configurewe can’t capture warnings using thecatch_warnings_for_itemfunction because it is not possible to have hook wrappers aroundpytest_configure.This function is mainly intended for plugins that need to issue warnings during
pytest_configure(or similar stages).Parameters:
warning (Warning)– Thewarninginstance.stacklevel (int)–stacklevelforwarded towarnings.warn.
addinivalue_line(
name, line)Add a line to an ini-file option. The option must have been declared but might not yet be set in which case the line becomes the first line in its value.
getini(
name)Return configuration value from an ini file.
If the specified name hasn’t been registered through a prior
parser.addinicall (usually from a plugin), a ValueError is raised.getoption(
name, default=<NOTSET>, skip=False)Return command line option value.
Parameters:
name (str)– Name of the option. You may also specify the literal--OPToption instead of the “dest” option name.default– Default value if no option of that name exists.skip (bool)– If True, raise pytest.skip if option does not exists or has a None value.
getvalue(
name, path=None)Deprecated, use
getoption()instead.getvalueorskip(
name, path=None)Deprecated, use
getoption(skip=True)instead.
ExceptionInfo
final classExceptionInfo Wraps
sys.exc_info()objects and offers help for navigating the traceback.classmethodfrom_exception(exception, exprinfo=None)Return an ExceptionInfo for an existing exception.
The exception must have a non-
None__traceback__attribute, otherwise this function fails with an assertion error. This means that the exception must have been raised, or added a traceback with thewith_traceback()method.Parameters:
exprinfo (Optional[str])– A text string helping to determine if we should stripAssertionErrorfrom the output. Defaults to the exceptionmessage/__str__().
New in version 7.4.
classmethodfrom_exc_info(exc_info, exprinfo=None)Like
from_exception(), but using old-style exc_info tuple.classmethodfrom_current(exprinfo=None)Return an
ExceptionInfomatching the current traceback.Warning
Experimental API
Parameters:
exprinf (Optional[str])– A text string helping to determine if we should strip -AssertionErrorfrom the output. Defaults to the exceptionmessage/__str__().
classmethodfor_later()Return an unfilled ExceptionInfo.
fill_unfilled(
exc_info)Fill an unfilled ExceptionInfo created with
for_later().propertytype:Type[E]The exception class.
propertyvalue:EThe exception value.
propertytb:TracebackTypeThe exception raw traceback.
propertytypename:strThe type name of the exception.
propertytraceback:TracebackThe traceback.
exconly(
tryshort=False)Return the exception as a string.
When ‘tryshort’ resolves to True, and the exception is an AssertionError, only the actual exception part of the exception representation is returned (so ‘AssertionError: ‘ is removed from the beginning).
errisinstance(
exc)Return True if the exception is an instance of exc.
Consider using
isinstance(excinfo.value, exc)instead.getrepr(
showlocals=False, style='long', abspath=False, tbfilter=True, funcargs=False, - truncate_locals=True, chain=True)Return
str()able representation of this exception info.Parameters:
showlocals (bool)– Show locals per traceback entry. Ignored ifstyle=="native".style (str)– long|short|line|no|native|value traceback style.abspath (bool)– If paths should be changed to absolute or left unchanged.tbfilter (Union[bool, Callable[[ExceptionInfo[BaseException]], Traceback]])– A filter for traceback entries.If false, don’t hide any entries.
If true, hide internal entries and entries that contain a local variable
__tracebackhide__ = True.If a callable, delegates the filtering to the callable.
Ignored if
styleis"native".funcargs (bool)– Show fixtures (“funcargs” for legacy purposes) per traceback entry.truncate_locals (bool)– Withshowlocals==True, make sure locals can be safely represented as strings.chain (bool)– If chained exceptions in Python 3 should be shown.
Changed in version 3.9: Added the
chainparameter.match(
regexp)Check whether the regular expression
regexpmatches the string representation of the exception usingre.search().If it matches
Trueis returned, otherwise anAssertionErroris raised.
ExitCode
final classExitCode(value) Encodes the valid exit codes by pytest.
Currently users and plugins may supply other exit codes as well.
New in version 5.0.
OK = 0
Tests passed.
TESTS_FAILED = 1
Tests failed.
INTERRUPTED = 2
pytest was interrupted.
INTERNAL_ERROR = 3
An internal error got in the way.
USAGE_ERROR = 4
pytest was misused.
NO_TESTS_COLLECTED = 5
pytest couldn’t find tests.
FixtureDef
final classFixtureDef - Bases:
Generic[FixtureValue]
A container for a fixture definition.
Note: At this time, only explicitly documented fields and methods are considered public stable API.
propertyscope:Literal['session', 'package', 'module', 'class', 'function']Scope string, one of
“function”,“class”,“module”,“package”,“session”.
- Bases:
MarkDecorator
classMarkDecorator A decorator for applying a mark on test functions and classes.
MarkDecoratorsare created withpytest.mark:pythonmark1 = pytest.mark.NAME # Simple MarkDecorator mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecoratormark1 = pytest.mark.NAME # Simple MarkDecorator mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecoratorand can then be applied as decorators to test functions:
python@mark2 def test_function(): pass@mark2 def test_function(): passWhen a
MarkDecoratoris called, it does the following:If called with a single class as its only positional argument and no additional keyword arguments, it attaches the mark to the class so it gets applied automatically to all test cases found in that class.
If called with a single function as its only positional argument and no additional keyword arguments, it attaches the mark to the function, containing all the arguments already stored internally in the
MarkDecorator.When called in any other case, it returns a new
MarkDecoratorinstance with the originalMarkDecorator’scontent updated with the arguments passed to this call.
Note: The rules above prevent a
MarkDecoratorfrom storing only a single function or class reference as its positional argument with no additional keyword or positional arguments. You can work around this by usingwith_args().propertyname:strAlias for
mark.name.propertyargs:Tuple[Any, ...]Alias for
mark.args.propertykwargs:Mapping[str, Any]Alias for
mark.kwargs.with_args(
*args, **kwargs)Return a
MarkDecoratorwith extra arguments added.Unlike calling the MarkDecorator,
with_args()can be used even if the sole argument is acallable/class.
MarkGenerator
final classMarkGenerator Factory for
MarkDecoratorobjects - exposed as apytest.marksingleton instance.Example:
pythonimport pytest @pytest.mark.slowtest def test_function(): passimport pytest @pytest.mark.slowtest def test_function(): passapplies a ‘slowtest’
Markontest_function.
Mark
final classMark A pytest mark.
name:
strName of the mark.
args:
Tuple[Any, ...]Positional arguments of the mark decorator.
kwargs:
Mapping[str, Any]Keyword arguments of the mark decorator.
combined_with(
other)Return a new Mark which is a combination of this Mark and another Mark.
Combines by appending args and merging kwargs.
Parameters:
other (Mark)– The mark to combine with.
Return type:
Mark
Metafunc
final classMetafunc Objects passed to the
pytest_generate_testshook.They help to inspect a test function and to generate tests according to test configuration or values specified in the class or module where a test function is defined.
definition
Access to the underlying
_pytest.python.FunctionDefinition.config
Access to the
pytest.Configobject for the test session.module
The module object where the test function is defined in.
function
Underlying Python test function.
fixturenames
Set of fixture names required by the test function.
cls
Class object where the test function is defined in or
None.parametrize(
argnames, argvalues, indirect=False, ids=None, scope=None, *, _param_mark=None)Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources see about setting indirect to do it rather than at test setup time.
Can be called multiple times, in which case each call parametrizes all previous parametrizations, e.g.
shellunparametrized: t parametrize ["x", "y"]: t[x], t[y] parametrize [1, 2]: t[x-1], t[x-2], t[y-1], t[y-2]unparametrized: t parametrize ["x", "y"]: t[x], t[y] parametrize [1, 2]: t[x-1], t[x-2], t[y-1], t[y-2]Parameters:
argnames (Union[str, Sequence[str]])– A comma-separated string denoting one or more argument names, or a list/tuple of argument strings.argvalues (Iterable[Union[ParameterSet, Sequence[object], object]])– The list of argvalues determines how often a test is invoked with different argument values. If only one argname was specified argvalues is a list of values. If N argnames were specified, argvalues must be a list of N-tuples, where each tuple-element specifies a value for its respective argname.indirect (Union[bool, Sequence[str]])– A list of arguments’ names (subset of argnames) or a boolean. If True the list contains all names from the argnames. Each argvalue corresponding to an argname in this list will be passed as request.param to its respective argname fixture function so that it can perform more expensive setups during the setup phase of a test rather than at collection time.ids (Optional[Union[Iterable[Optional[object]], Callable[[Any], Optional[object]]]])– Sequence of (or generator for) ids forargvalues, or a callable to return part of the id for each argvalue. With sequences (and generators likeitertools.count()) the returned ids should be of typestring,int,float,bool, orNone. They are mapped to the corresponding index inargvalues.Nonemeans to use the auto-generated id. If it is a callable it will be called for each entry inargvalues, and the return value is used as part of the auto-generated id for the whole set (where parts are joined with dashes (“-“)). This is useful to provide more specific ids for certain items, e.g. dates. ReturningNonewill use an auto-generated id. If no ids are provided they will be generated automatically from the argvalues.scope (Optional[Literal['session', 'package', 'module', 'class', 'function']])– If specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration.
Parser
final classParser Parserfor command line arguments andini-file values.Variables:
extra_info– Dict of generic param -> value to display in case there’s an error processing the command line arguments.
getgroup(
name, description='', after=None)Get (or create) a named option Group.
Parameters:
name (str)– Name of the option group.description (str)– Long description for –help output.after (Optional[str])– Name of another group, used for ordering –help output.
Returns: The option group.
Return type:
OptionGroupThe returned group object has an
addoptionmethod with the same signature asparser.addoptionbut will be shown in the respective group in the output ofpytest --help.addoption(
*opts, **attrs)Register a command line option.
Parameters:
opts (str)– Option names, can be short or long options.attrs (Any)– Same attributes as the argparse library’sadd_argument()function accepts.
After command line parsing, options are available on the pytest config object via
config.option.NAMEwhereNAMEis usually set by passing adestattribute, for exampleaddoption("--long", dest="NAME", ...).parse_known_args(
args, namespace=None)Parse the known arguments at this point.
Returns: An argparse namespace object.
Return type:
Namespaceparse_known_and_unknown_args(
args, namespace=None)Parse the known arguments at this point, and also return the remaining unknown arguments.
Returns: A tuple containing an argparse namespace object for the known arguments, and a list of the unknown arguments.
Return type:
Tuple[Namespace, List[str]]addini(
name, help, type=None, default=None)Register an ini-file option.
Parameters:
name (str)– Name of the ini-variable.type (Optional[Literal['string', 'paths', 'pathlist', 'args', 'linelist', 'bool']])– Type of the variable. Can be:string: a stringbool: a booleanargs: a list of strings, separated as in a shelllinelist: a list of strings, separated by line breakspaths: a list ofpathlib.Path, separated as in a shellpathlist: a list ofpy.path, separated as in a shell
New in version 7.0: The
pathsvariable type.Defaults to
stringifNoneor not passed.default (Optional[Any])– Default value if no ini-file option exists but is queried.
The value of ini-variables can be retrieved via a call to
config.getini(name).
OptionGroup
classOptionGroup A group of options shown in its own section.
addoption(
*opts, **attrs)Add an option to this group.
If a shortened version of a long option is specified, it will be suppressed in the help.
addoption('--twowords', '--two-words')results in help showing--two-wordsonly, but--twowordsgets accepted and the automatic destination is inargs.twowords.Parameters:
opts (str)– Option names, can be short or long options.attrs (Any)– Same attributes as the argparse library’sadd_argument()function accepts.
PytestPluginManager
final classPytestPluginManager Bases:
PluginManagerA
pluggy.PluginManagerwith additional pytest-specific functionality:Loading plugins from the command line,
PYTEST_PLUGINSenv variable andpytest_pluginsglobal variables found in plugins being loaded.conftest.pyloading during start-up.
parse_hookimpl_opts(
plugin, name)parse_hookspec_opts(
module_or_class, name)register(
plugin, name=None)Register a plugin and return its name.
If a name is not specified, a name is generated using
get_canonical_name().If the name is blocked from registering, returns
None.If the plugin is already registered, raises a
ValueError.getplugin(
name)hasplugin(
name)Return whether a plugin with the given name is registered.
import_plugin(
modname, consider_entry_points=False)Import a plugin with
modname.If
consider_entry_pointsisTrue, entry point names are also considered to find a plugin.add_hookcall_monitoring(
before, after)Add before/after tracing functions for all hooks.
Returns an undo function which, when called, removes the added tracers.
before(hook_name, hook_impls, kwargs)will be called ahead of all hook calls and receive a - hookcaller instance, a list of HookImpl instances and the keyword arguments for the hook call.after(outcome, hook_name, hook_impls, kwargs)receives the same arguments asbeforebut also a -_Resultobject which represents the result of the overall hook call.add_hookspecs(
module_or_class)Add new hook specifications defined in the given
module_or_class.Functions are recognized as hook specifications if they have been decorated with a matching
HookspecMarker.check_pending()
Verify that all hooks which have not been verified against a hook specification are optional, otherwise raise
PluginValidationError.enable_tracing()
Enable tracing of hook calls.
Returns an undo function which, when called, removes the added tracing.
get_canonical_name(
plugin)Return a canonical name for a plugin object.
Note that a plugin may be registered under a different name specified by the caller of
register(plugin, name). To obtain the name of n registered plugin useget_name(plugin)instead.get_hookcallers(
plugin)Get all hook callers for the specified plugin.
get_name(
plugin)Return the name the plugin is registered under, or
Noneif is isn’t.get_plugin(
name)Return the plugin registered under the given name, if any.
get_plugins()
Return a set of all registered plugin objects.
has_plugin(
name)Return whether a plugin with the given name is registered.
is_blocked(
name)Return whether the given plugin name is blocked.
is_registered(
plugin)Return whether the plugin is already registered.
list_name_plugin()
Return a list of (name, plugin) pairs for all registered plugins.
list_plugin_distinfo()
Return a list of (plugin, distinfo) pairs for all setuptools-registered plugins.
load_setuptools_entrypoints(
group, name=None)Load modules from querying the specified setuptools
group.Parameters:
group (str)– Entry point group to load plugins.name (str)– If given, loads only plugins with the givenname.
Return type:
intReturns: The number of plugins loaded by this call.
set_blocked(
name)Block registrations of the given name, unregister if already registered.
subset_hook_caller(
name, remove_plugins)Return a proxy
_hooks._HookCallerinstance for the named method which manages calls to all registered plugins except the ones fromremove_plugins.unregister(
plugin=None, name=None)Unregister a plugin and all of its hook implementations.
The plugin can be specified either by the plugin object or the plugin name. If both are specified, they must agree.
TestReport
final classTestReport - Bases:
BaseReport
Basic test report object (also used for setup and teardown calls if they fail).
Reports can contain arbitrary extra attributes.
nodeid:
strNormalized collection nodeid.
location:
Tuple[str, Optional[int], str]A
(filesystempath, lineno, domaininfo)tuple indicating the actual location of a test item - it might be different from the collected one e.g. if a method is inherited from a different module. The filesystempath may be relative toconfig.rootdir. The line number is 0-based.keywords:
Mapping[str, Any]A name -> value dictionary containing all keywords and markers associated with a test invocation.
outcome:
Literal['passed', 'failed', 'skipped']Test outcome, always one of “passed”, “failed”, “skipped”.
longrepr:
Union[None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr]None or a failure representation.
when:
Optional[str]One of ‘setup’, ‘call’, ‘teardown’ to indicate runtest phase.
user_properties
User properties is a list of tuples (name, value) that holds user defined properties of the test.
sections:
List[Tuple[str, str]]Tuples of str
(heading, content)with extra information for the test report. Used by pytest to add text captured fromstdout,stderr, and intercepted logging events. May be used by other plugins to add arbitrary information to reports.duration:
floatTime it took to run just the test.
start:
floatThe system time when the call started, in seconds since the epoch.
stop:
floatThe system time when the call ended, in seconds since the epoch.
classmethodfrom_item_and_call(item, call)Create and fill a TestReport with standard item and call info.
Parameters:
item (Item)– The item.call (CallInfo[None])– The call info.
propertycaplog:strReturn captured log lines, if log capturing is enabled.
New in version 3.5.
propertycapstderr:strReturn captured text from
stderr, if capturing is enabled.New in version 3.0.
propertycapstdout:strReturn captured text from
stdout, if capturing is enabled.New in version 3.0.
propertycount_towards_summary:boolExperimental Whether this report should be counted towards the totals shown at the end of the test session: “1 passed, 1 failure, etc”.
Note
This function is considered experimental, so beware that it is subject to changes even in patch releases.
propertyfailed:boolWhether the outcome is failed.
propertyfspath:strThe path portion of the reported node, as a string.
propertyhead_line:Optional[str]Experimental The head line shown with longrepr output for this report, more commonly during traceback representation during failures:
shell________ Test.foo ________________ Test.foo ________In the example above, the head_line is “Test.foo”.
Note
This function is considered experimental, so beware that it is subject to changes even in patch releases.
propertylongreprtext:strRead-only property that returns the full string representation of
longrepr.New in version 3.0.
propertypassed:boolWhether the outcome is passed.
propertyskipped:boolWhether the outcome is skipped.
- Bases:
TestShortLogReport
classTestShortLogReport Used to store the test status result category, shortletter and verbose word. For example
"rerun", "R", ("RERUN", {"yellow": True}).Variables:
category– The class of result, for example“passed”,“skipped”,“error”, or the empty string.letter– The short letter shown as testing progresses, for example".","s","E", or the empty string.word– Verbose word is shown as testing progresses in verbose mode, for example"PASSED","SKIPPED","ERROR", or the empty string.
category:
strAlias for field number 0
letter:
strAlias for field number 1
word:
Union[str, Tuple[str, Mapping[str, bool]]]Alias for field number 2
_Result
Result object used within hook wrappers, see _Result in the pluggy documentation for more information.
Stash
classStash Stashis a type-safe heterogeneous mutable mapping that allows keys and value types to be defined separately from where it (theStash) is created.Usually you will be given an object which has a
Stash, for exampleConfigor aNode:pythonstash: Stash = some_object.stashstash: Stash = some_object.stashIf a module or plugin wants to store data in this
Stash, it createsStashKeysfor its keys (at the module level):python# At the top-level of the module some_str_key = StashKey[str]() some_bool_key = StashKey[bool]()# At the top-level of the module some_str_key = StashKey[str]() some_bool_key = StashKey[bool]()To store information:
python# Value type must match the key. stash[some_str_key] = "value" stash[some_bool_key] = True# Value type must match the key. stash[some_str_key] = "value" stash[some_bool_key] = TrueTo retrieve the information:
python# The static type of some_str is str. some_str = stash[some_str_key] # The static type of some_bool is bool. some_bool = stash[some_bool_key]# The static type of some_str is str. some_str = stash[some_str_key] # The static type of some_bool is bool. some_bool = stash[some_bool_key]setitem(
key, value)Set a value for key.
getitem(
key)Get the value for key.
Raises
KeyErrorif the key wasn’t set before.get(
key, default)Get the value for key, or return default if the key wasn’t set before.
setdefault(
key, default)Return the value of key if already set, otherwise set the value of key to default and return - default.
delitem(
key)Delete the value for key.
Raises
KeyErrorif the key wasn’t set before.contains(
key)Return whether key was set.
len()
Return how many items exist in the stash.
classStashKey - Bases:
Generic[T]
StashKeyis an object used as a key to aStash.A
StashKeyis associated with the typeTof the value of the key.A
StashKeyis unique and cannot conflict with another key.- Bases: