Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install python plugin before running pytest tests?

Tags:

I need to install a python plugin that is a simple python file before the starting tests using pytest. I have used entry_points in setup.py. My problem is a bit complex so let's get into the problem with an example and we will come back to the problem later.

There are two packages- one is core and another one is mypackage.

Core provided functionality to add a plugin with group name 'my.plugin'.

core package logic

from importlib_metadata import entry_points 
def plugin_method(some_data):
    plugins = entry_points()['my.plugin']
    loaded_plugins = []
    for p in plugins:
       loaded_plugins.apend(p.load())
    #Does some processing on the data and decides which method to call from plugin
    #call plugin method
return result

mypackage logic

setup.py

setup(
...
entry_points={'my.plugin': 'plugin1= plugin1.logic'}
...
)

logic.py

def method1(method_data):
    print('method1 called')
    return 1

def method2(method_data):
    print('method1 called')
    return 2

main.py

def method_uses_plugin()
    # create data
    plugin_method(data)

The plugin works fine. :)


Problem

I have written a test case for the method_uses_plugin method. It works fine if I have installed pypackage on my machine but it fails if installation is not done (in jenkins pipeline 🙁 )

We don't usually install the package to run test cases because test cases should use source code directly.

We might need to do something with pytest to register the plugin in entry_points. I have tried many links but nothing worked.

My use case a bit complex but a similar question can be found here

like image 650
Ashok Rayal Avatar asked Jun 25 '21 13:06

Ashok Rayal


2 Answers

There is two usecase to run the test on the actual source code.

In your Local machine

If you want to test the source code while working, you can simply install your package in editable mode with the command:

pip install -e .

Documentation of -e from the man page:

-e,--editable <path/url>
     Install a project in editable mode (i.e.  setuptools "develop mode") from a local project path or a VCS url.

This will link the package to the . location of the code, meaning that any change made to the source code will be reflected on the package.

In Continuous Integration (CI)

As your CI is running on a docker container, you can simply copy the source code inside it, install it with pip install . and finally run pytest.

like image 51
Antoine Dubuis Avatar answered Sep 30 '22 16:09

Antoine Dubuis


If all else fails you can try to convert your code into an executable and use batch commands to run pip install for as many packages as you need and then run your program. I believe in Jenkins you can run batch files as an administrator.

Invoke pip install from batch file

Run Batch file as an administrator in Jenkins

like image 36
Shay Ribera Avatar answered Sep 30 '22 16:09

Shay Ribera