我在编写一个自定义插件时遇到问题,该插件用于在 rst 格式的文件中收集 py.test 结果。最后,我想将此文件包含到 Sphinx 文档中。
我已经在当前工作目录中的 myplugin.py 模块中编写了我的插件。然后我调用
py.test -p myplugin ../mytool/test
以 ImportError: No module named myplugin 结尾。
我也试过这样调用它
py.test -p .myplugin ../mytool/test
类似于相对导入,但没有成功。
如何正确使用插件?
Docs提供以下有关插件检测的信息:
Plugin discovery order at tool startup pytest loads plugin modules at tool startup in the following way:
by loading all builtin plugins
by loading all plugins registered through setuptools entry points.
by pre-scanning the command line for the -p name option and loading the specified plugin before actual command line parsing.
by loading all conftest.py files as inferred by the command line invocation:
if no test paths are specified use current dir as a test path if exists, load conftest.py and test*/conftest.py relative to the directory part of the first test path. Note that pytest does not find conftest.py files in deeper nested sub directories at tool startup. It is usually a good idea to keep your conftest.py file in the top level test or project root directory.
- by recursively loading all plugins specified by the pytest_plugins variable in conftest.py files
请您参考如下方法:
如果插件不可导入,那是因为它不在 sys.path 上。尝试使用 PYTHONPATH 变量显式添加它:
PYTHONPATH=/path/to/dir/of/myplugin py.test -p myplugin ../mytool/test
