errbotio / errbot

Errbot is a chatbot, a daemon that connects to your favorite chat service and bring your tools and some fun into the conversation.
http://errbot.io
GNU General Public License v3.0
3.08k stars 607 forks source link

About test instance method #260

Closed hanks closed 9 years ago

hanks commented 10 years ago

I use code below to test instance method:

class MyPluginInstanceMethodTests(FullStackTest):
    def setUp(self):
        me = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
        # Adding /la/bla to path is needed because of the path mangling
        # FullStackTest does on extra_test_file.
        plugin_dir = os.path.join(me, 'la', 'bla')
        # Call our parent's setUp() method but pass our directory to
        # extra_test_file so our plugin is loaded.
        super(MyPluginInstanceMethodTests, self).setUp(extra_test_file=plugin_dir)

    def test_mycommand_another_helper(self):
        plugin = plugin_manager.get_plugin_obj_by_name('MyBot') # always None
        expected = "This is another awesome command"
        result = plugin.mycommand_another_helper()
        self.assertEqual(result, expected)

When I run err.py -T, it works fine, but during the test, variable plugin is always None. And also no plugins loaded when I run plugin_manager.get_all_plugins().

PS: the plugin folder is a subdirectory of plugin path defined in config.py.

zoni commented 10 years ago

Could you provide the (DEBUG) output that is generated during the test run? It should contain debug output from YAPSY (the plugin system Err uses) which should show which directories are being searched and what is being found.

I believe that's a good place to start to find where this might be going wrong.

hanks commented 10 years ago

oh, there is a BOT_LOG_LEVEL in the config.py, change it to logging.DEBUG, and can show the DEBUG log. Thank you for the hint.

This hint should be in the tutorial:)

hanks commented 10 years ago

@zoni Here is debug log for test run, but I still can not figure out what's wrong with the test case.

EINFO:root:Try to open db file /hanks/workspace/err/data/core.db
DEBUG:root:Opened shelf of TestBackend
DEBUG:root:created the thread pool<errbot.bundled.threadpool.ThreadPool object at 0x109074050>
DEBUG:yapsy:PluginFileLocator walks (recursively) into directory: /hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins
DEBUG:yapsy:__init__.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:PluginFileLocator found a candidate:
    /hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins/chatRoom.plug
DEBUG:yapsy:chatRoom.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:chatRoom.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:PluginFileLocator found a candidate:
    /hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins/vcheck.plug
DEBUG:yapsy:vcheck.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:vcheck.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:PluginFileLocator found a candidate:
    /hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins/webserver.plug
DEBUG:yapsy:webserver.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:webserver.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:wsview.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:wsview.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:webstatus.html is not a valid plugin for strategy info_ext
DEBUG:yapsy:PluginFileLocator walks (recursively) into directory: /hanks/workspace/kemonobot/plugins
DEBUG:yapsy:.DS_Store is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:plugin_base.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:plugin_base.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:test_utils.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:test_utils.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:utils.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:utils.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:__init__.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:PluginFileLocator found a candidate:
    /hanks/workspace/kemonobot/plugins/kemonobot/kemonobot.plug
DEBUG:yapsy:kemonobot.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:kemonobot.pyc is not a valid plugin for strategy info_ext
DEBUG:yapsy:test_kemonobot.py is not a valid plugin for strategy info_ext
DEBUG:yapsy:test_kemonobot.pyc is not a valid plugin for strategy info_ext
INFO:root:webhooks:  Bind /echo/ to echo
DEBUG:root:serve from <errbot.backends.test.TestBackend object at 0x10905bd10>
INFO:root:Activate internal commands
INFO:root:Activating all the plugins...
DEBUG:root:All plugins: ChatRoom, KemonoBot, VersionChecker, Webserver
INFO:root:Activate plugin: ChatRoom
INFO:root:Activating ChatRoom with min_err_version = 2.0.0 and max_version = 2.0.0
DEBUG:root:Templates directory found for this plugin [/hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins/templates]
DEBUG:yapsy:Activating plugin: bots.ChatRoom
DEBUG:root:Init storage for ChatRoom
DEBUG:root:Loading /hanks/workspace/err/data/plugins/ChatRoom.db
INFO:root:Try to open db file /hanks/workspace/err/data/plugins/ChatRoom.db
DEBUG:root:Opened shelf of ChatRoom
DEBUG:root:Adding command : room_create -> room_create
INFO:root:Activate plugin: KemonoBot
INFO:root:Activating KemonoBot with min_err_version = 2.0.0 and max_version = 2.0.0
DEBUG:root:No templates directory found for this plugin [Looking for /hanks/workspace/kemonobot/plugins/kemonobot/templates]
DEBUG:yapsy:Activating plugin: bots.KemonoBot
DEBUG:root:Init storage for KemonoBot
DEBUG:root:Loading /hanks/workspace/err/data/plugins/KemonoBot.db
INFO:root:Try to open db file /hanks/workspace/err/data/plugins/KemonoBot.db
DEBUG:root:Opened shelf of KemonoBot
DEBUG:root:Adding command : fab -> fab
INFO:root:Activate plugin: VersionChecker
INFO:root:Activating VersionChecker with min_err_version = 2.0.0 and max_version = 2.0.0
DEBUG:root:Templates directory found for this plugin [/hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins/templates]
DEBUG:yapsy:Activating plugin: bots.VersionChecker
DEBUG:root:Checking version
DEBUG:root:Programming the polling of version_check every 86400 seconds with args [] and kwargs {}
DEBUG:root:Init storage for VersionChecker
DEBUG:root:Loading /hanks/workspace/err/data/plugins/VersionChecker.db
INFO:root:Try to open db file /hanks/workspace/err/data/plugins/VersionChecker.db
DEBUG:root:Opened shelf of VersionChecker
INFO:root:Activate plugin: Webserver
INFO:root:Activating Webserver with min_err_version = 2.0.0 and max_version = 2.0.0
DEBUG:root:Templates directory found for this plugin [/hanks/.pyvirtualenvs/kemono/lib/python2.7/site-packages/errbot/builtins/templates]
DEBUG:yapsy:Activating plugin: bots.Webserver
INFO:root:Webserver is not configured. Forbid activation
INFO:root:
INFO:root:Notifying connection to all the plugins...
DEBUG:root:All plugins: ChatRoom, KemonoBot, VersionChecker, Webserver
DEBUG:root:Callback <yapsy_loaded_plugin_ChatRoom_0.ChatRoom object at 0x10a217f10>
INFO:root:Callback_connect
INFO:root:Join room err@conference.server.tld as user Err
DEBUG:root:Callback <yapsy_loaded_plugin_KemonoBot_0.KemonoBot object at 0x10a9b8e50>
DEBUG:root:Callback <yapsy_loaded_plugin_VersionChecker_0.VersionChecker object at 0x10a221910>
INFO:root:Plugin activation done.
DEBUG:root:Adding command : about -> about
DEBUG:root:Adding command : apropos -> apropos
DEBUG:root:Adding command : blacklist -> blacklist
DEBUG:root:Adding command : config -> config
DEBUG:root:Adding command : echo -> echo
DEBUG:root:Adding command : export_configs -> export_configs
DEBUG:root:Adding command : help -> help
DEBUG:root:Adding command : history -> history
DEBUG:root:Adding command : import_configs -> import_configs
DEBUG:root:Adding command : load -> load
DEBUG:root:Adding command : log_tail -> log_tail
DEBUG:root:Adding command : reload -> reload
DEBUG:root:Adding command : repos -> repos
DEBUG:root:Adding command : repos_export -> repos_export
DEBUG:root:Adding command : repos_install -> repos_install
DEBUG:root:Adding command : repos_uninstall -> repos_uninstall
DEBUG:root:Adding command : repos_update -> repos_update
DEBUG:root:Adding command : restart -> restart
DEBUG:root:Adding command : status -> status
DEBUG:root:Adding command : unblacklist -> unblacklist
DEBUG:root:Adding command : unload -> unload
DEBUG:root:Adding command : uptime -> uptime
DEBUG:root:Adding command : zap_configs -> zap_configs
INFO:root:Stop magic message received, quitting...
DEBUG:root:Trigger disconnect callback
INFO:root:Disconnect callback, deactivating all the plugins.
DEBUG:root:All plugins: ChatRoom, KemonoBot, VersionChecker, Webserver
DEBUG:yapsy:Deactivating plugin: bots.ChatRoom
DEBUG:root:Closed shelf of ChatRoom
DEBUG:yapsy:Deactivating plugin: bots.KemonoBot
DEBUG:root:Closed shelf of KemonoBot
DEBUG:yapsy:Deactivating plugin: bots.VersionChecker
DEBUG:root:You still have active pollers at deactivation stage, I cleaned them up for you.
DEBUG:root:Closed shelf of VersionChecker
DEBUG:root:Trigger shutdown
INFO:root:Shutdown.
DEBUG:root:Closed shelf of TestBackend
INFO:root:Bye.
INFO:root:Main bot thread quits

======================================================================
ERROR: test_build_command (__main__.KemonoBotTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_kemonobot.py", line 23, in test_build_command
    result = plugin.build_command('fab', 'hello')
AttributeError: 'NoneType' object has no attribute 'build_command'

----------------------------------------------------------------------
Ran 1 test in 1.614s

FAILED (errors=1)
zoni commented 10 years ago

Hmmmmm.. It seems to find your plugin just fine:

DEBUG:yapsy:PluginFileLocator walks (recursively) into directory: /hanks/workspace/kemonobot/plugins

DEBUG:yapsy:PluginFileLocator found a candidate:
    /hanks/workspace/kemonobot/plugins/kemonobot/kemonobot.plug

INFO:root:Activate plugin: KemonoBot
INFO:root:Activating KemonoBot with min_err_version = 2.0.0 and max_version = 2.0.0
DEBUG:root:No templates directory found for this plugin [Looking for /hanks/workspace/kemonobot/plugins/kemonobot/templates]
DEBUG:yapsy:Activating plugin: bots.KemonoBot
DEBUG:root:Init storage for KemonoBot
DEBUG:root:Loading /hanks/workspace/err/data/plugins/KemonoBot.db
INFO:root:Try to open db file /hanks/workspace/err/data/plugins/KemonoBot.db
DEBUG:root:Opened shelf of KemonoBot
DEBUG:root:Adding command : fab -> fab

plugin_manager.get_plugin_obj_by_name('KemonoBot') should get you the plugin object at that point. What's the code in your unittests?

hanks commented 10 years ago

@zoni Thank you for your reply. Here is the test code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest
from errbot.backends.test import FullStackTest, pushMessage, popMessage
from errbot import plugin_manager

class KemonoBotTests(FullStackTest):
    """Test suite for kemono bot
    """

    def setUp(self):
        me = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
        # Adding /la/bla to path is needed because of the path mangling
        # FullStackTest does on extra_test_file.
        plugin_dir = os.path.join(me, 'la', 'bla')
        super(KemonoBotTests, self).setUp(extra_test_file=plugin_dir)

    def test_build_command(self):
        plugin = plugin_manager.get_plugin_obj_by_name('KemonoBot')
        expected = "fab hello"
        result = plugin.build_command('fab', 'hello')
        self.assertEqual(result, expected)

if __name__ == '__main__': # pragma: no cover
    unittest.main()

The directory trees like:

-- plugins
   -- kemonobot
      -- kemonobot.py
      -- kemonobot.plug
      -- test_kemonobot.py
hanks commented 10 years ago

@zoni I solved this problem by rewrite all the codes, and check test work incrementally. and the solution is you should add a dummy pushMessage() test method in your test file...

def test_activate_plugin(self):
    pushMessage('')
zoni commented 9 years ago

Hmmm... That is odd. That shouldn't be necessary. But glad it's working!