【python】之pytest命令行调用(二)

二 如何用命令行调用pytest

See also

Complete pytest command-line flag reference

也可以点击链接查看完整的命令行介绍(那可太全面了)

In general, pytest is invoked with the command pytest (see below for other ways to invoke pytest). This will execute all tests in all files whose names follow the form test_*.py or *_test.py in the current directory and its subdirectories. More generally, pytest follows standard test discovery rules.

通常,pytest是用命令pytest调用的(参见下面的其他调用pytest的方法)。这将执行当前目录及其子目录中所有文件名以test_*.py或*_test.py形式结尾的文件中的所有测试。更一般地说,pytest遵循标准的测试发现规则

2.1 指定要运行的测试

Pytest 支持多种从命令行运行和选择测试的方法。

Run tests in a module:在模块中运行测试 (模块就是py文件)

1
pytest test_mod.py

Run tests in a directory:在目录中运行测试

1
pytest testing/

Run tests by keyword expressions:通过关键字表达式运行测试

1
pytest -k 'MyClass and not method'

This will run tests which contain names that match the given string expression (case-insensitive), which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple. Use “” instead of ‘’ in expression when running this on Windows

这将运行包含与给定名称匹配的名称的测试 字符串表达式 (不区分大小写),它可以包括使用文件名、类名和函数名作为变量的Python运算符。上面的例子将运行 TestMyClass.test_something 但不是 TestMyClass.test_method_simple .

(这个用的少)

Run tests by collection arguments:通过集合参数运行测试

Pass the module filename relative to the working directory, followed by specifiers like the class name and function name separated by :: characters, and parameters from parameterization enclosed in [].

传递相对于工作目录的模块文件名,后跟由 :: 字符分隔的类名和函数名等说明符,以及包含在 [ ] 中的参数化参数。(下面让我们看看 :: 的用法)

To run a specific test within a module:要在模块内运行特定测试:

1
pytest tests/test_mod.py::test_func

To run all tests in a class: 要运行类中的所有测试:

1
pytest tests/test_mod.py::TestClass

Specifying a specific test method: 指定具体的测试方法:

1
pytest tests/test_mod.py::TestClass::test_method

Specifying a specific parametrization of a test: 指定测试的特定参数化:

1
pytest tests/test_mod.py::test_func[x1,y2]

Run tests by marker expressions:通过标记表达式运行测试

1
pytest -m slow 

Will run all tests which are decorated with the @pytest.mark.slow decorator.

For more information see marks.

将运行所有用 @pytest.mark.slow 装饰器装饰的测试。 有关更多信息,请参阅标记(关于mark标记,后面会讲…)。

Run tests from packages:从包运行测试

1
pytest --pyargs pkg.testing

This will import pkg.testing and use its filesystem location to find and run tests from.

这将导入 pkg.testing 并使用其文件系统位置来查找并运行测试。(这个用的少)

2.2 获取有关版本、选项名称、环境变量的帮助

1
2
3
pytest --version   # shows where pytest was imported from 版本
pytest --fixtures # show available builtin function arguments显示可用的内置函数参数
pytest -h | --help # show help on command line and config file options显示帮助

2.3 分析测试执行持续时间

6.0版本更改

To get a list of the slowest 10 test durations over 1.0s long要获取长度超过 1.0 秒的最慢 10 个测试持续时间的列表:

1
pytest --durations=10 --durations-min=1.0 # durations持续时间

By default, pytest will not show test durations that are too small (<0.005s) unless -vv is passed on the command-line.

默认情况下,pytest 不会显示太短(<0.005s)的测试持续时间,除非在命令行上传递 -vv。

2.4 管理插件的加载

2.4.1 早期加载插件

You can early-load plugins (internal and external) explicitly in the command-line with the -p option:

可以使用 -p 选项在命令行中显式提前加载插件(内部和外部):

1
pytest -p mypluginmodule

The option receives a name parameter, which can be:

该选项接收一个名称参数,该参数可以是:

  • A full module dotted name, for example myproject.plugins. This dotted name must be importable.

  • 完整的模块点名称,例如 myproject.plugins。这个点名称必须是可导入的。

  • The entry-point name of a plugin. This is the name passed to setuptools when the plugin is registered. For example to early-load the pytest-cov plugin you can use:

  • 插件的入口点名称。这是注册插件时传递给 setuptools 的名称。例如,要提前加载 pytest-cov 插件,您可以使用:

    1
    pytest -p pytest_cov

2.4.2 禁用插件

To disable loading specific plugins at invocation time, use the -p option together with the prefix no:.

要禁止在调用时加载特定插件,请使用 -p 选项和前缀 no:。

示例:要禁用加载插件 doctest(该插件负责从文本文件执行 doctest 测试),请像这样调用 pytest:

1
pytest -p no:doctest

2.5 调用 pytest 的其他方式

2.5.1 过 python -m pytest 调用 pytest

可以从命令行通过 Python 解释器调用测试

1
python -m pytest [...]

This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.path.

这几乎相当于直接调用命令行脚本 pytest […],只不过通过 python 调用还会将当前目录添加到 sys.path 中。

2.5.2 从 Python 代码调用 pytest【常用】

可以直接从Python代码调用pytest:

1
retcode = pytest.main()

this acts as if you would call “pytest” from the command line. It will not raise SystemExit but return the exit code instead. If you don’t pass it any arguments, main reads the arguments from the command line arguments of the process (sys.argv), which may be undesirable. You can pass in options and arguments explicitly:

这就像从命令行调用“pytest”一样。它不会引发SystemExit,而是返回退出代码。如果不向它传递任何参数,则main将从进程的命令行参数(sys.argv)中读取参数,这可能是不希望看到的。你可以显式地传入选项和参数:

1
retcode = pytest.main(["-x", "mytestdir"])

您可以为 pytest.main 指定其他插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
# content of myinvoke.py
import sys

import pytest


class MyPlugin:
def pytest_sessionfinish(self):
print("*** test run reporting finishing")


if __name__ == "__main__":
sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()]))

Running it will show that MyPlugin was added and its hook was invoked:

运行它将显示 MyPlugin 已添加并且其钩子已被调用:

1
2
$ python myinvoke.py
*** test run reporting finishing

Note

Calling pytest.main() will result in importing your tests and any modules that they import. Due to the caching mechanism of python’s import system, making subsequent calls to pytest.main() from the same process will not reflect changes to those files between the calls. For this reason, making multiple calls to pytest.main() from the same process (in order to re-run tests, for example) is not recommended.

注意
调用pytest.main()将导致导入测试和它们导入的任何模块。由于python导入系统的缓存机制,从同一进程对pytest.main()的后续调用将不会反映调用之间对这些文件的更改。出于这个原因,不建议从同一进程多次调用pytest.main()(例如,为了重新运行测试)。


【python】之pytest命令行调用(二)
http://example.com/2024/01/21/662python之pytest命令行调用(二)/
作者
Wangxiaowang
发布于
2024年1月21日
许可协议