Jay Joshi
Jay Joshi

Reputation: 1562

pytester - testdir can not find the pytest plugin

I have created a sample plugin. I want to test it with pytester pluging. Doc reference:https://docs.pytest.org/en/latest/writing_plugins.html#testing-plugins

However, the pytester does not seem to find the plugin which needs to be tested.

Please find the file_setup I have done.

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

Please find the files I have created

setup.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"]

tests/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

The output I am getting::

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 =====

Why testdir can not find the plugin?

Upvotes: 3

Views: 1633

Answers (2)

msudder
msudder

Reputation: 512

i have tried using testdir.plugins and what I normally find is that I need to run w/ a -p, for your case...

testdir.runpytest('-p', 'path.to.plugin.module', '--jay', 'bob')

alternatively, modifying pytest_plugins may also work

pytest_plugins = ['_pytest.pytester', 'path.to.jay']

Upvotes: 0

Jay Joshi
Jay Joshi

Reputation: 1562

I figured that the requirement for the pytester and testdir fixture is that the plugin should be installed in the python environment.

To install the develop(current) version of your plugin you can use -e param with pip.

pip install -e <path_to_plugin>

Or you can do similar using setup.py

python setup.py develop

Once the plugin is installed, executing the steps in the question solves the problem.

Upvotes: 2

Related Questions