python - pytester - testdir 找不到 pytest 插件

标签 python pytest

我创建了一个示例插件。我想用 pytester 插件来测试它。文档引用:https://docs.pytest.org/en/latest/writing_plugins.html#testing-plugins

但是,pytester似乎没有找到需要测试的插件。

请找到我已经完成的file_setup。

poc_plugin
|- pytest_myplugin
|  |- plugin.py   
|- setup.py
|- pytest.ini
|- tests
   |- conftest.py
   |- test_myplugin.py

请找到我创建的文件

设置.py

from setuptools import setup, find_packages
setup(
    name="pytest-myplugin",
    include_package_data=True,
    python_requires=">=3.0, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
    install_requires=[
        "pytest>=5.3.5",
    ],
    setup_requires=["pytest-runner"],
    classifiers=[
        "Framework :: Pytest",
    ],
    packages=find_packages(include=["pytest_myplugin", "pytest_myplugin.*"]),
    test_suite="tests",
    entry_points={"pytest11": ["myplugin = pytest_myplugin.plugin"]},
    version="0.1.0",
)

pytest_myplugin/plugin.py

import pytest

def pytest_addoption(parser):
    """
    This is a pytest hook to add options from the command line.
    """
    group = parser.getgroup("pytest-jay")

    group.addoption(
        "--jay",
        action="store",
        dest="jay",
        default="package",
        help="A Sample option",
    )

conftest.py

pytest_plugins  = ["pytester"]

测试/test_plugin.pt

import pytest


test_sample_txt = """
def test_sample():
    assert True
    """

def test_pluging_one(testdir):
    """Make sure that pytest accepts our fixture."""

    # create a temporary pytest test module
    testdir.makepyfile(test_sample_txt)

    # run pytest with the following cmd args
    result = testdir.runpytest(
        '--jay=jay',
    )

    # fnmatch_lines does an assertion internally
    result.stdout.fnmatch_lines([
        '*::test_sample PASSED*',
    ])

    # make sure that that we get a '0' exit code for the testsuite
    assert result.ret == 0

pytest.ini

[pytest]
testpaths = tests

我得到的输出::

C:\Jay\Work\poc_plugin\tests\test_plugin.py:23: Failed
----------------------------------------------------------------------------------------- Captured stderr call ------------------------------------------------------------------------------------------
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --jay=jay
  inifile: None
  rootdir: C:\Users\jay.joshi\AppData\Local\Temp\pytest-of-jay.joshi\pytest-46\test_pluging_one0

======================================================================================== short test summary info ========================================================================================
FAILED tests/test_plugin.py::test_pluging_one - Failed: remains unmatched: '*::test_sample PASSED*'
=========================================================================================== 1 failed in 0.26s =====

为什么testdir找不到插件?

最佳答案

我认为对 pytestertestdir 固定装置的要求是插件应该安装在 python 环境中。

要安装插件的开发(当前)版本,您可以将-e参数与pip一起使用。

pip install -e <path_to_plugin>

或者您可以使用setup.py进行类似的操作

python setup.py develop

安装插件后,执行问题中的步骤即可解决问题。

关于python - pytester - testdir 找不到 pytest 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60861746/

相关文章:

python - 从 pytest fixture 返回多个对象

python - pytest mocker fixture mock 模块来自定义的位置而不是使用的位置

python - 全局变量未绑定(bind)局部错误

python - os.path.join 失败,显示 "TypeError: object of type ' LocalPath' has no len()"

python - pytest - 从运行测试的 CLI 命令指定日志级别

python - 尝试模块化 python 代码但出现递归错误 : maximum recursion depth

python - 使用 pytest fixtures 的测试可以交互运行吗?

python - 将包含字符串和整数值的元组中的值组合起来并存储为数据帧

python - Drive-SDK Python 代码无法通过选定的标题词获取驱动文件

python - 谷歌应用引擎: store uploaded file in Google Cloud Storage