如何在运行pytest测试之前安装python插件?

在使用 pytest 开始测试之前,我需要安装一个 python 插件,它是一个简单的 python 文件。我在 setup.py 中使用了 entry_points。我的问题有点复杂,让我们通过一个例子来解决这个问题,我们稍后再回到这个问题。

有两个包 - 一个是core,另一个是mypackage

核心提供了添加名为“ my.plugin ”的插件的功能。

核心封装逻辑

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

我的包逻辑

设置文件

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

逻辑文件

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

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

主文件

def method_uses_plugin()
    # create data
    plugin_method(data)

该插件工作正常。:)


问题

我已经为method_uses_plugin方法编写了一个测试用例。如果我在我的机器上安装了pypackage,它工作正常,但如果安装没有完成,它会失败(在 jenkins 管道中)

我们通常不安装包来运行测试用例,因为测试用例应该直接使用源代码。

我们可能需要对 pytest 做一些事情来在 entry_points 中注册插件。我尝试了很多链接,但没有任何效果。

我的用例有点复杂,但可以在这里找到类似的问题

以上是如何在运行pytest测试之前安装python插件?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>