_pytest.config package¶
Submodules¶
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
-
-
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.
-
staticmethod
-
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_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]¶
-
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 andpytest_plugins
global variables found in plugins being loaded;conftest.py
loading during start-up;
-
register
(plugin, name=None)[source]¶ Register a plugin and return its canonical name or
None
if the name is blocked from registering. Raise aValueError
if the plugin is already registered.
-
_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.
-
_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.
-
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:
-
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
pluginmanager (PytestPluginManager) –
invocation_params (InvocationParams) – Object containing the parameters regarding the
pytest.main
invocation.
-
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 theaddopts
ini option are handled by pytest, not being included in theargs
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
-
pluginmanager
¶ the plugin manager handles plugin registration and hook invocation.
- Type
-
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).
-
pytest_cmdline_parse
(args: List[str]) → _pytest.config.Config[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.
-
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.
-
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.
-
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.