_pytest.config package

Module contents

command line options, ini-file and conftest.py processing.

_PluggyPlugin

alias of builtins.object

class ExitCode(value)[source]

Bases: enum.IntEnum

New in version 5.0.

Encodes the valid exit codes by pytest.

Currently users and plugins may supply other exit codes as well.

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

exception ConftestImportFailure(path, excinfo)[source]

Bases: Exception

main(args=None, plugins=None) → Union[int, _pytest.config.ExitCode][source]

return exit code, after performing an in-process test run.

Parameters
  • args – list of command line arguments.

  • plugins – list of plugin objects to be auto-registered during initialization.

class cmdline[source]

Bases: object

staticmethod main(args=None, plugins=None) → Union[int, _pytest.config.ExitCode]

return exit code, after performing an in-process test run.

Parameters
  • args – list of command line arguments.

  • plugins – list of plugin objects to be auto-registered during initialization.

filename_arg(path, optname)[source]

Argparse type validator for filename arguments.

Path

path of filename

Optname

name of the option

directory_arg(path, optname)[source]

Argparse type validator for directory arguments.

Path

path of directory

Optname

name of the option

get_config(args=None, plugins=None)[source]
get_plugin_manager()[source]

Obtain a new instance of the _pytest.config.PytestPluginManager, with default plugins already loaded.

This function can be used by integration with other tools, like hooking into pytest to run tests into an IDE.

_prepareconfig(args: Optional[Union[py._path.local.LocalPath, Sequence[Union[str, py._path.local.LocalPath]]]] = None, plugins=None)_pytest.config.Config[source]
_fail_on_non_top_pytest_plugins(conftestpath, confcutdir)[source]
class PytestPluginManager[source]

Bases: pluggy.manager.PluginManager

Overwrites pluggy.PluginManager to add pytest-specific functionality:

  • loading plugins from the command line, PYTEST_PLUGINS env variable and pytest_plugins global variables found in plugins being loaded;

  • conftest.py loading during start-up;

parse_hookimpl_opts(plugin, name)[source]
parse_hookspec_opts(module_or_class, name)[source]
register(plugin, name=None)[source]

Register a plugin and return its canonical name or None if the name is blocked from registering. Raise a ValueError if the plugin is already registered.

getplugin(name)[source]
hasplugin(name)[source]

Return True if the plugin with the given name is registered.

pytest_configure(config)[source]
_set_initial_conftests(namespace)[source]

load initial conftest files given a preparsed “namespace”. As conftest files may add their own command line options which have arguments (‘–my-opt somepath’) we might get some false positives. All builtin and 3rd party plugins will have been loaded, however, so common options will not confuse our logic here.

_try_load_conftest(anchor)[source]
_getconftestmodules(path)[source]
_rget_with_confmod(name, path)[source]
_importconftest(conftestpath)[source]
consider_preparse(args, *, exclude_only=False)[source]
consider_pluginarg(arg)[source]
consider_conftest(conftestmodule)[source]
consider_env()[source]
consider_module(mod)[source]
_import_plugin_specs(spec)[source]
import_plugin(modname, consider_entry_points=False)[source]

Imports a plugin with modname. If consider_entry_points is True, entry point names are also considered to find a plugin.

_get_plugin_specs_as_list(specs)[source]

Parses a list of “plugin specs” and returns a list of plugin names.

Plugin specs can be given as a list of strings separated by “,” or already as a list/tuple in which case it is returned as a list. Specs can also be None in which case an empty list is returned.

_ensure_removed_sysmodule(modname)[source]
class Notset[source]

Bases: object

for ... in _iter_rewritable_modules(package_files)[source]

Given an iterable of file names in a source distribution, return the “names” that should be marked for assertion rewrite (for example the package “pytest_mock/__init__.py” should be added as “pytest_mock” in the assertion rewrite mechanism.

This function has to deal with dist-info based distributions and egg based distributions (which are still very much in use for “editable” installs).

Here are the file names as seen in a dist-info based distribution:

pytest_mock/__init__.py pytest_mock/_version.py pytest_mock/plugin.py pytest_mock.egg-info/PKG-INFO

Here are the file names as seen in an egg based distribution:

src/pytest_mock/__init__.py src/pytest_mock/_version.py src/pytest_mock/plugin.py src/pytest_mock.egg-info/PKG-INFO LICENSE setup.py

We have to take in account those two distribution flavors in order to determine which names should be considered for assertion rewriting.

More information:

https://github.com/pytest-dev/pytest-mock/issues/167

class Config(pluginmanager: _pytest.config.PytestPluginManager, *, invocation_params: _pytest.config.Config.InvocationParams = None)[source]

Bases: object

Access to configuration values, pluginmanager and plugin hooks.

Parameters
class InvocationParams(args, plugins, dir: pathlib.Path)[source]

Bases: object

Holds parameters passed during pytest.main()

The object attributes are read-only.

New in version 5.1.

Note

Note that the environment variable PYTEST_ADDOPTS and the addopts ini option are handled by pytest, not being included in the args attribute.

Plugins accessing InvocationParams must be aware of that.

args

tuple of command-line arguments as passed to pytest.main().

plugins

list of extra plugins, might be None.

dir

directory where pytest.main() was invoked from.

option

access to command line option as attributes.

Type

argparse.Namespace

pluginmanager

the plugin manager handles plugin registration and hook invocation.

Type

PytestPluginManager

invocation_dir

Backward compatibility

add_cleanup(func)[source]

Add a function to be called when the config object gets out of use (usually coninciding with pytest_unconfigure).

_do_configure()[source]
_ensure_unconfigure()[source]
get_terminal_writer()[source]
pytest_cmdline_parse(args: List[str])_pytest.config.Config[source]
notify_exception(excinfo, option=None)[source]
cwd_relative_nodeid(nodeid: str)str[source]
classmethod fromdictargs(option_dict, args)[source]

constructor usable for subprocesses.

_processopt(opt: Argument)None[source]
pytest_load_initial_conftests(early_config)[source]
_initini(args: Sequence[str])None[source]
_consider_importhook(args: Sequence[str])None[source]

Install the PEP 302 import hook if using assertion rewriting.

Needs to parse the –assert=<mode> option from the commandline and find all the installed plugins to mark them for rewriting by the importhook.

_mark_plugins_for_rewrite(hook)[source]

Given an importhook, mark for rewrite any top-level modules or packages in the distribution package for all pytest plugins.

_validate_args(args: List[str], via: str) → List[str][source]

Validate known args.

_preparse(args: List[str], addopts: bool = True)None[source]
_checkversion()[source]
parse(args: List[str], addopts: bool = True)None[source]
addinivalue_line(name, line)[source]

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 the first line in its value.

getini(name: str)[source]

return configuration value from an ini file. If the specified name hasn’t been registered through a prior parser.addini call (usually from a plugin), a ValueError is raised.

_getini(name: str) → Any[source]
_getconftest_pathlist(name, path)[source]
_get_override_ini_value(name: str) → Optional[str][source]
getoption(name: str, default=<NOTSET>, skip: bool = False)[source]

return command line option value.

Parameters
  • name – name of the option. You may also specify the literal --OPT option instead of the “dest” option name.

  • default – default value if no option of that name exists.

  • skip – if True raise pytest.skip if option does not exists or has a None value.

getvalue(name, path=None)[source]

(deprecated, use getoption())

getvalueorskip(name, path=None)[source]

(deprecated, use getoption(skip=True))

_assertion_supported()[source]
_warn_about_missing_assertion(mode)[source]
create_terminal_writer(config: _pytest.config.Config, *args, **kwargs)_pytest._io.TerminalWriter[source]

Create a TerminalWriter instance configured according to the options in the config object. Every code which requires a TerminalWriter object and has access to a config object should use this function.

_strtobool(val)[source]

Convert a string representation of truth to true (1) or false (0).

True values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’. Raises ValueError if ‘val’ is anything else.

Note

copied from distutils.util