Skip to content

Testing plugins

Meet Mangukiya edited this page Jul 2, 2017 · 3 revisions

Using the pytest fixture that errbot provides

errbot provides a pytest fixture that can be used to get a minimal errbot instance to test the errbot functionality.

You push in a message -> you pop a message -> assert the popped message with expected output.

Example:

pytest_plugins = ['errbot.backends.test'] # to load the pytest fixture

def test_function(testbot):
   testbot.push_message(...)
   assert '...' in testbot.pop_message

# testbot is the fixture name. pytest fixtures are used by using the same name as the name of the fixture, then pytest passes the fixture in place of that argument. ;)

Using helper methods in your plugins instead

# Plugin

class Plugin(BotPlugin):

   @botcmd
   def cmd(self, msg, args):
      ...
      # call the helper method appropriately
      Plugin.helper(...)

   @staticmethod
   def helper(...):
      # do something that doesn't require the plugin object
      ...

# PluginTest

import Plugin

def test_plug():
   assert Plugin.helper(...) == ...

Using TestBot class

You can directly use TestBot backend:

from errbot.backends.test import TestBot

testbot = TestBot(extra_plugin_dir='plugins', extra_config={..whatever extra config, if any..}, loglevel=logging.ERROR)

Loading the plugin manually to patch stuffs in the plugin class

Several plugins can only be tested by mocking. One can monkey patch most of the stuff with python namespace. However, if you want to patch something in a plugin, it isn't as simple as:

import plugin

plugin.whatever.namespace.that.has.to.be.patched = monkey_patch

since the plugins are dynamically loaded by yapsy. Yes, errbot uses external plugin manager. And since yapsy loads plugins dynamically it isn't easy to patch plugins since you don't know the namespace.

Here's a workaround:

from errbot.backends.test import TestBot

PluginClass.to.patch = monkey_patch

testbot = TestBot(extra_plugin_dir='plugins')
testbot.start()

plugin = testbot.bot.plugin_manager.get_plugin_obj_by_name('PluginClass')
plugin.stuff.to.monkey.patch = monkey_patch
# patch it all away