pipoteam / pipobot

A modular bot for Jabber MUCs.
16 stars 9 forks source link

AsyncModule tests #17

Open nim65s opened 8 years ago

nim65s commented 8 years ago

Is there a way to write unit tests for AsyncModules ?

sleduc commented 8 years ago

Not really sure how to do it in the master branch, but it is quite easy in the python3 branch : see commit https://github.com/pipoteam/pipobot/commit/50b3b731f0f399b79a6e86a0aa10480eee762414

The idea is that the bot_jabber module used to manage answers to messages by creating a thread for each message. With commit https://github.com/pipoteam/pipobot/commit/a7e16c04e9a861fe69680f627fc2592d1395c5c7 I moved the thread management from bot_jabber to the parent class bot, so the bot_jabber and the bot_test behave the same way.

In test mode, there is an output attribute to the bot, which is a python Queue. Each message is written in the Queue, even if it is done in a Thread. So even if you use an async module, all you have to do is create your module, and then in your test, just call the get() method of the Queue to retrieve messages. Since get() is blocking, it will wait for your AsyncModule to do call bot.say to be unlocked, and you can verify the content of the message, the delay, etc.