Usage and Invocations
https://docs.pytest.org/en/6.2.0/usage.html
1. Calling pytest through python -m pytest
You can invoke testing through the Python interpreter from the command line:
python -m pytest [...]
This is almost equivalent to invoking the command line script:
pytest [...]
Differences: calling via python will also add the current directory to sys.path.
2. Calling pytest from Python code
You can invoke pytest from Python code directly:
pytest.main()
You can pass in options and arguments:
pytest.main(["-x", "mytestdir"])
Source code of main():
def main(
args: Optional[Union[List[str], py.path.local]] = None,
plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
) -> Union[int, ExitCode]:
"""Perform an in-process test run.
:param args: List of command line arguments.
:param plugins: List of plugin objects to be auto-registered during initialization.
:returns: An exit code.
"""
Arguments & Options
You can check the arguments by typing "pytest --help". Here lists some of the arguments in LeTP tests.
$ pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
[options]
- reporting:
-r chars
show extra test summary info as specified by chars:
(f)ailed, (E)rror, (s)kipped, (x)failed,
(X)passed, (p)assed, (P)assed with output, (a)ll except passed (p/P),
or (A)ll. (w)arnings are enabled by default (see --disable-warnings),
'N' can be used to reset the list. (default: 'fE').
--color=color
color terminal output (yes/no/auto).
-v, --verbose
increase verbosity.
-
pytest-warnings:
--rootdir=ROOTDIR
Define root directory for tests. Can be
relative path:
'root_dir', './root_dir', 'root_dir/another_dir/';
absolute path:
'/home/user/root_dir'; path with variables: '$HOME/root_dir'.
-c file
load configuration fromfile
instead of trying to locate one of the implicit configuration files.
-
test session debugging and configuration:
-p name
early-load given plugin module name or entry point (multi-allowed).
-
logging:
--log-file=LOG_FILE
path to a file when logging will be written to.