Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load custom plugin in py.test

Tags:

python

pytest

I am having an issue with a custom plugin I am writing to collect py.test results in a rst-formatted file. In the end I would like to include this file into Sphinx docs.

I have written my plugin in the myplugin.py module which is in the current working directory. I then call

py.test -p myplugin ../mytool/test

which ends in an ImportError: No module named myplugin.

I also tried calling it like

py.test -p .myplugin ../mytool/test

like a relative import, but without success.

How can I properly use the plugin?

The Docs give the following information about plugin detection:

Plugin discovery order at tool startup pytest loads plugin modules at tool startup in the following way:

  • by loading all builtin plugins

  • by loading all plugins registered through setuptools entry points.

  • by pre-scanning the command line for the -p name option and loading the specified plugin before actual command line parsing.

  • by loading all conftest.py files as inferred by the command line invocation:

if no test paths are specified use current dir as a test path if exists, load conftest.py and test*/conftest.py relative to the directory part of the first test path. Note that pytest does not find conftest.py files in deeper nested sub directories at tool startup. It is usually a good idea to keep your conftest.py file in the top level test or project root directory.

  • by recursively loading all plugins specified by the pytest_plugins variable in conftest.py files
like image 833
GorillaPatch Avatar asked Feb 13 '26 15:02

GorillaPatch


1 Answers

If the plugin is not importable it is because it is not on sys.path. Try explicitly adding it using the PYTHONPATH variable:

PYTHONPATH=/path/to/dir/of/myplugin py.test -p myplugin ../mytool/test
like image 188
flub Avatar answered Feb 15 '26 04:02

flub