在pytest

时间:2017-01-20 09:45:01

标签: python pytest

我正在尝试编写一个稍后将被其他人使用的插件。设置和命名使我感到困惑。作为一个基础,我克隆了pytest-xdist并删除了大多数似乎无关紧要的文件。管理以使插件运行,虽然这些名称对我来说似乎是随机的。

setup.py:

from setuptools import setup

setup(
    name="groceries",
    platforms=['linux', 'osx', 'win32'],
    packages=['groceries'],
    entry_points={
        'pytest11': [
            'groceries.potato = groceries.plugin',
            'groceries.tomato = groceries.tomato'
        ],
    },
    zip_safe=False,
    install_requires=['execnet>=1.1', 'pytest>=2.7.0', 'py>=1.4.22'],
    setup_requires=['setuptools_scm'],
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Framework :: Pytest',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Operating System :: POSIX',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: MacOS :: MacOS X',
        'Topic :: Software Development :: Testing',
        'Topic :: Software Development :: Quality Assurance',
        'Topic :: Utilities',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
    ],
)

杂货/ plugin.py:

import py
import pytest


def pytest_addoption(parser):
    print "pytest_addoption"
    group = parser.getgroup("test", "distributed and subprocess testing")
    group.addoption(
        '--some-text', action="append", default="")


def pytest_addhooks(pluginmanager):
    print "pytest_addhooks"


@pytest.mark.trylast
def pytest_configure(config):
    print "pytest_configure"


@pytest.mark.tryfirst
def pytest_cmdline_main(config):
    print "pytest_cmdline_main"

杂货/ tomato.py:

import py
import pytest


def pytest_addoption(parser):
    print "pytest_addoption tomato"
    group = parser.getgroup("test", "distributed and subprocess testing")
    group.addoption(
        '--other-text', action="append", default="")


def pytest_addhooks(pluginmanager):
    print "pytest_addhooks tomato"


@pytest.mark.trylast
def pytest_configure(config):
    print "pytest_configure tomato"


@pytest.mark.tryfirst
def pytest_cmdline_main(config):
    print "pytest_cmdline_main tomato"

我在sudo python setup.py install的根目录中使用setup.py进行安装。

itos-MacBook-Pro-2:pytest-xdist ito$ pytest --trace-config
pytest_addhooks
pytest_addoption
pytest_addhooks tomato
pytest_addoption tomato
pytest_cmdline_main tomato
pytest_cmdline_main
PLUGIN registered: <_pytest.config.PytestPluginManager object at 0x10239ab50>
PLUGIN registered: <_pytest.config.Config object at 0x1028b7490>
PLUGIN registered: <module '_pytest.mark' from '/Library/Python/2.7/site-packages/_pytest/mark.pyc'>
PLUGIN registered: <module '_pytest.main' from '/Library/Python/2.7/site-packages/_pytest/main.pyc'>
PLUGIN registered: <module '_pytest.terminal' from '/Library/Python/2.7/site-packages/_pytest/terminal.pyc'>
PLUGIN registered: <module '_pytest.runner' from '/Library/Python/2.7/site-packages/_pytest/runner.pyc'>
PLUGIN registered: <module '_pytest.python' from '/Library/Python/2.7/site-packages/_pytest/python.pyc'>
PLUGIN registered: <module '_pytest.fixtures' from '/Library/Python/2.7/site-packages/_pytest/fixtures.pyc'>
PLUGIN registered: <module '_pytest.debugging' from '/Library/Python/2.7/site-packages/_pytest/debugging.pyc'>
PLUGIN registered: <module '_pytest.unittest' from '/Library/Python/2.7/site-packages/_pytest/unittest.pyc'>
PLUGIN registered: <module '_pytest.capture' from '/Library/Python/2.7/site-packages/_pytest/capture.pyc'>
PLUGIN registered: <module '_pytest.skipping' from '/Library/Python/2.7/site-packages/_pytest/skipping.pyc'>
PLUGIN registered: <module '_pytest.tmpdir' from '/Library/Python/2.7/site-packages/_pytest/tmpdir.pyc'>
PLUGIN registered: <module '_pytest.monkeypatch' from '/Library/Python/2.7/site-packages/_pytest/monkeypatch.pyc'>
PLUGIN registered: <module '_pytest.recwarn' from '/Library/Python/2.7/site-packages/_pytest/recwarn.pyc'>
PLUGIN registered: <module '_pytest.pastebin' from '/Library/Python/2.7/site-packages/_pytest/pastebin.pyc'>
PLUGIN registered: <module '_pytest.helpconfig' from '/Library/Python/2.7/site-packages/_pytest/helpconfig.pyc'>
PLUGIN registered: <module '_pytest.nose' from '/Library/Python/2.7/site-packages/_pytest/nose.pyc'>
PLUGIN registered: <module '_pytest.assertion' from '/Library/Python/2.7/site-packages/_pytest/assertion/__init__.pyc'>
PLUGIN registered: <module '_pytest.junitxml' from '/Library/Python/2.7/site-packages/_pytest/junitxml.pyc'>
PLUGIN registered: <module '_pytest.resultlog' from '/Library/Python/2.7/site-packages/_pytest/resultlog.pyc'>
PLUGIN registered: <module '_pytest.doctest' from '/Library/Python/2.7/site-packages/_pytest/doctest.pyc'>
PLUGIN registered: <module '_pytest.cacheprovider' from '/Library/Python/2.7/site-packages/_pytest/cacheprovider.pyc'>
PLUGIN registered: <module '_pytest.freeze_support' from '/Library/Python/2.7/site-packages/_pytest/freeze_support.pyc'>
PLUGIN registered: <module '_pytest.setuponly' from '/Library/Python/2.7/site-packages/_pytest/setuponly.pyc'>
PLUGIN registered: <module '_pytest.setupplan' from '/Library/Python/2.7/site-packages/_pytest/setupplan.pyc'>
PLUGIN registered: <module 'groceries.plugin' from '/Library/Python/2.7/site-packages/groceries-0.0.0-py2.7.egg/groceries/plugin.py'>
PLUGIN registered: <module 'groceries.tomato' from '/Library/Python/2.7/site-packages/groceries-0.0.0-py2.7.egg/groceries/tomato.py'>
PLUGIN registered: <module 'pytest_cov.plugin' from '/Library/Python/2.7/site-packages/pytest_cov/plugin.py'>
PLUGIN registered: <module 'pytest_html.plugin' from '/Library/Python/2.7/site-packages/pytest_html/plugin.py'>
PLUGIN registered: <module 'xdist.boxed' from '/Library/Python/2.7/site-packages/xdist/boxed.py'>
PLUGIN registered: <module 'xdist.looponfail' from '/Library/Python/2.7/site-packages/xdist/looponfail.py'>
PLUGIN registered: <module 'xdist.plugin' from '/Library/Python/2.7/site-packages/xdist/plugin.py'>
PLUGIN registered: <_pytest.capture.CaptureManager instance at 0x102d4a5f0>
PLUGIN registered: <module 'conftest' from '/Users/ito/dev/python/pytest-xdist/testing/conftest.py'>
PLUGIN registered: <Session 'pytest-xdist'>
PLUGIN registered: <_pytest.cacheprovider.LFPlugin instance at 0x102d61368>
PLUGIN registered: <_pytest.terminal.TerminalReporter instance at 0x102d617a0>
pytest_configure
pytest_configure tomato
PLUGIN registered: <_pytest.fixtures.FixtureManager instance at 0x102d74200>
========================================================================================================================= test session starts ==========================================================================================================================
platform darwin -- Python 2.7.10, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
using: pytest-3.0.5 pylib-1.4.32
setuptools registered plugins:
  groceries-0.0.0 at /Library/Python/2.7/site-packages/groceries-0.0.0-py2.7.egg/groceries/plugin.py
  groceries-0.0.0 at /Library/Python/2.7/site-packages/groceries-0.0.0-py2.7.egg/groceries/tomato.py
  pytest-cov-2.4.0 at /Library/Python/2.7/site-packages/pytest_cov/plugin.py
  pytest-html-1.13.0 at /Library/Python/2.7/site-packages/pytest_html/plugin.py
  pytest-xdist-1.15.0 at /Library/Python/2.7/site-packages/xdist/boxed.py
  pytest-xdist-1.15.0 at /Library/Python/2.7/site-packages/xdist/looponfail.py
  pytest-xdist-1.15.0 at /Library/Python/2.7/site-packages/xdist/plugin.py
active plugins:
    cacheprovider       : /Library/Python/2.7/site-packages/_pytest/cacheprovider.pyc
    helpconfig          : /Library/Python/2.7/site-packages/_pytest/helpconfig.pyc
    pytestconfig        : <_pytest.config.Config object at 0x1028b7490>
    debugging           : /Library/Python/2.7/site-packages/_pytest/debugging.pyc
    runner              : /Library/Python/2.7/site-packages/_pytest/runner.pyc
    unittest            : /Library/Python/2.7/site-packages/_pytest/unittest.pyc
    xdist.looponfail    : /Library/Python/2.7/site-packages/xdist/looponfail.py
    xdist               : /Library/Python/2.7/site-packages/xdist/plugin.py
    capturemanager      : <_pytest.capture.CaptureManager instance at 0x102d4a5f0>
    pastebin            : /Library/Python/2.7/site-packages/_pytest/pastebin.pyc
    skipping            : /Library/Python/2.7/site-packages/_pytest/skipping.pyc
    session             : <Session 'pytest-xdist'>
    lfplugin            : <_pytest.cacheprovider.LFPlugin instance at 0x102d61368>
    fixtures            : /Library/Python/2.7/site-packages/_pytest/fixtures.pyc
    tmpdir              : /Library/Python/2.7/site-packages/_pytest/tmpdir.pyc
    capture             : /Library/Python/2.7/site-packages/_pytest/capture.pyc
    terminalreporter    : <_pytest.terminal.TerminalReporter instance at 0x102d617a0>
    assertion           : /Library/Python/2.7/site-packages/_pytest/assertion/__init__.pyc
    mark                : /Library/Python/2.7/site-packages/_pytest/mark.pyc
    terminal            : /Library/Python/2.7/site-packages/_pytest/terminal.pyc
    groceries.tomato    : /Library/Python/2.7/site-packages/groceries-0.0.0-py2.7.egg/groceries/tomato.py
    4332301136          : <_pytest.config.PytestPluginManager object at 0x10239ab50>
    main                : /Library/Python/2.7/site-packages/_pytest/main.pyc
    nose                : /Library/Python/2.7/site-packages/_pytest/nose.pyc
    groceries.potato    : /Library/Python/2.7/site-packages/groceries-0.0.0-py2.7.egg/groceries/plugin.py
    xdist.boxed         : /Library/Python/2.7/site-packages/xdist/boxed.py
    python              : /Library/Python/2.7/site-packages/_pytest/python.pyc
    /Users/ito/dev/python/pytest-xdist/testing/conftest.py: /Users/ito/dev/python/pytest-xdist/testing/conftest.py
    recwarn             : /Library/Python/2.7/site-packages/_pytest/recwarn.pyc
    funcmanage          : <_pytest.fixtures.FixtureManager instance at 0x102d74200>
    monkeypatch         : /Library/Python/2.7/site-packages/_pytest/monkeypatch.pyc
    resultlog           : /Library/Python/2.7/site-packages/_pytest/resultlog.pyc
    setuponly           : /Library/Python/2.7/site-packages/_pytest/setuponly.pyc
    html                : /Library/Python/2.7/site-packages/pytest_html/plugin.py
    pytest_cov          : /Library/Python/2.7/site-packages/pytest_cov/plugin.py
    junitxml            : /Library/Python/2.7/site-packages/_pytest/junitxml.pyc
    doctest             : /Library/Python/2.7/site-packages/_pytest/doctest.pyc
    setupplan           : /Library/Python/2.7/site-packages/_pytest/setupplan.pyc
    freeze_support      : /Library/Python/2.7/site-packages/_pytest/freeze_support.pyc
rootdir: /Users/ito/dev/python/pytest-xdist, inifile: 
plugins: groceries-0.0.0, cov-2.4.0, html-1.13.0, xdist-1.15.0
collected 1 items 

testing/test_stuff.py .

所以看起来它们已经被加载并且钩子被执行了。现在我的问题是- p带来的错误。

$ pytest -p groceries --trace-config

1在0.02秒内通过

不知道这应该做什么,因为杂货未被宣布为插件,尽管它是一个包。

$ pytest -p potato --trace-config

1传递0.03秒

有道理

$ pytest -p tomato --trace-config

ImportError:导入插件时出错&#34;番茄&#34;:没有名为tomato的模块

$ pytest -p groceries.potato --trace-config

ImportError:导入插件时出错&#34; groceries.potato&#34;:没有名为potato的模块

$ pytest -p groceries.tomato --trace-config

1在0.02秒内通过

因此,如果您希望potato正常工作,则需要调用插件文件-p groceries.potato。但为什么简单的potato有效而不是tomato。是不是有一个名为potato的软件包漂浮在某个地方?那为什么tomato不起作用?

现在如果我$ sudo pip unistall groceries这5次传递都没有,我总是得到一个ImportError,所以我不认为我在其他包中声明了相同的插件。

0 个答案:

没有答案