Pytest authoritative guide 25 Configuration

Return: Pytest authoritative guide

Configuration

Command line options and configuration file settings

You can use the help option for help with routine options INI configuration file command line options and values:

pytest -h   # prints options _and_ config file settings

This command will display the installed plug-line registration and profile settings.

Initialization: Determine ROOTDIR and INIFILE

The rootdir pytest command line parameters (specified test file path), and the presence ini-files determined for each test run a. During startup, and the determined file rootdir ini file as part of the print pytest header.

The following is a summary of pytest use rootdir:

  • Nodeid during the collection configuration; each test is assigned a unique nodeid, rooted in the id, and taking full path RootDir, class name, function name, and parametric (if any).
  • Stores items used as plug / stable test location-specific information; for example, the internal cache in which to create a plug rootdir .pytest_cache subdirectory to store its cross-state test runs.

Particular emphasis is required, rootdir embodiment it is not used to modify imported sys.path / PYTHONPATH or influence module. For more detailed information, see: Pytest import mechanism and system paths.

--rootdir = path command-line option can be used to force a particular directory. It can be used in conjunction with pytest.ini file addopts item to add this feature to include specific directories to the environment variable.

Looking rootdir

This is to find rootdir algorithm args:

  • Determines whether the specified common ancestor directories that args file path is identified as present in the system. If you can not find such a path, the common ancestor directory as the current working directory.
  • Looking pytest.ini, tox.ini and setup.cfg in the parent directory and file up. If they match, it will be the ini file and its directory will be rootdir.
  • If the ini file is not found, please setup.py from a common ancestor look up directory to determine rootdir.
  • If no setup.py was found, looking for pytest.ini, tox.ini and setup.cfg in each of the specified args up. If they match, it will be the ini file and its directory will be rootdir.
  • If you can not find the ini file, it uses a common ancestor has been identified as the root directory. This structure allows the use of pytest packet does not belong to the ini file without any specific configuration.
  • If no args are given, pytest collect test in the current working directory, and from there determine rootdir.

Note: custom pytest plugin command line arguments may contain the path, as shown in FIG. Then it is obligatory or pytest use test.log Folder OK rootdir (see also issue 1435). You can also use a reference point to the current working directory. pytest --log-output ../../test.log argsargs.
Please note that the existing pytest.ini files will always be considered a match, but only if it contains tox.ini and setup.cfg match [pytest ] or [ Tool: pytest ] section, respectively. More ini-file candidate file option will never merge - the first winner (pytest.ini even if does not contain any [pytest] section, always successfully).

The object is then config with the following properties:

  • config.rootdir: root directory determined to ensure that there is.
  • config.inifile: ini file may be determined None.
    rootdir construct used as a reference test directory address ( "nodeids") can also be used by the widget information is stored for each test run.

E.g:

pytest path/to/testdir path/other/

Will identify common ancestor path, and then check the ini file as follows:

# first look for pytest.ini files
path/pytest.ini
path/tox.ini    # must also contain [pytest] section to match
path/setup.cfg  # must also contain [tool:pytest] section to match
pytest.ini
... # all the way down to the root

# now look for setup.py
path/setup.py
setup.py
... # all the way down to the root

How to change the default value of command-line options

Each time you use, type the same series of command-line options can be cumbersome pytest. For example, if you always want to see more information about the skipped tests and xfailed testing, and a brief "point" progress output, it can be written into the configuration file:

# content of pytest.ini or tox.ini
[pytest]
addopts = -ra -q

# content of setup.cfg
[tool:pytest]
addopts = -ra -q

In addition, you can set PYTEST_ADDOPTS environment variable to add the command line options when using the environment:

export PYTEST_ADDOPTS="-v"

This is the way to build the command line in the presence of addopts or environment variables:

<pytest.ini:addopts> $PYTEST_ADDOPTS <extra command-line arguments>

Therefore, if the user performs the command line:

pytest -m slow

The actual execution of the command line:

pytest -ra -q -v -m slow

Note that, as with other command-line application, in the case of options conflict, the last one will win, so the above example will show verbose output, because -voverwrites -q.

Built-in configuration file options

For a complete list of options, refer to the reference documentation.

Guess you like

Origin www.cnblogs.com/superhin/p/11741587.html