首页 > 解决方案 > 创建 pytest 插件

问题描述

我使用 pytest 对我的代码进行单元测试。关于这一点,我试图创建一个可以在整个目录中使用的 pytest 插件。我可以在主根目录中创建一个 conftest.py 文件,也可以创建一个可安装的 pytest 插件。我更喜欢第二种选择。

但是,pytest 文档确实为如何继续创建和安装用户定义的插件提供了很好的指南。我在父目录中创建了 setup.py 到我的 conftest.py 所在的目录。我的 setup.py 如下所示:

from setuptools import setup

setup(
    name="tryplugin",
    packages = ['pluginTry'],
    version = "1.0.0",

    # the following makes a plugin available to pytest
    entry_points = {
        'pytest11': [
            'name_of_plugin = pluginTry',
        ]
    },
    classifiers=["Framework :: Pytest"],
)

接下来我运行命令: python3 setup.py install

然后我的插件出现在pytest list

但是,我的 conftest.py 创建的命令行 pytest 选项没有出现在pytest -h. 我的灯具也不亮pytest --fixtures

相反,我收到以下错误:

During handling of the above exception, another exception occurred:
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/py/_path/local.py:718: in pyimport
    issame = self.samefile(modfile)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/py/_path/local.py:204: in samefile
    return py.error.checked_call(
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/py/_error.py:86: in checked_call
    raise cls("%s%r" % (func.__name__, args))
E   py.error.ENOTDIR: [Not a directory]: samefile('/Users/myName/Desktop/MainDirectory/pluginTry/conftest.py', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tryplugin-1.0.0-py3.8.egg/pluginTry/conftest.py')

标签: pythonpluginspytestsetup.py

解决方案


推荐阅读