我创建了一个示例插件。我想用 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找不到插件?
最佳答案
我认为对 pytester
和 testdir
固定装置的要求是插件应该安装在 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/