python-telegram-bot / ptbtest

GNU General Public License v3.0
50 stars 28 forks source link

Library not up to date #20

Closed hileamlakB closed 2 years ago

hileamlakB commented 3 years ago

I know, I know, I have already read the ReadMe. I just wanted a catchy title. So yesterday I was looking for a good test suit for the python-telegram-bot and boy I had a hard time finding any, the only best one I found was this one, but unfortunately it was out of date. So I had a delima. As I am a beginner to testing, I had no clue about mock testing, so I was wondering if it was easier for me to learn to do mock test while I am doing my project or try to fix up and use this already great Test liberary. Well contaemplating and looking for a better solution took about the whole day yesterday. And today in just a few hours I was able to fix up some parameters in this old liberary and to get my first test running. So I was hoping to see if there is still interest in this library by other developers and if there will be or is any effort in maintainig it. This will be my first big open source project to contribute, but I would like to try, and in case there are no plans for further improvement I could atleast do a gitpull with the few changes I made that worked for me so that some one noob like me could use them.

JosXa commented 3 years ago

How about https://github.com/JosXa/tgintegration? ;)

hileamlakB commented 3 years ago

I actually liked that one. The only reason I choose not to use it was because it doesn't support(correct me if am wrong) group and supergroup chats. And my bot is a game bot that is supposed to collect commands in group chats and answer accordingly in both private and group chats. So I wanted a package that can allow me to write unit tests to see what the bot responds to different commands. Thanks for pointing that out!! And correct me if I am wrong.

Deer-Spangle commented 3 years ago

Hey there, I use tgIntegration for some of my stuff, and you can actually build tests that use groupchats too. Here's an example of one of my tests using a groupchat, you just pass the group id in as the peer argument to controller.collect(): https://github.com/Deer-Spangle/FA-search-bot/blob/37af664660bd02de32708844a5dbdbc70ad14c7c/fa_search_bot/tests/integration/test_neaten.py#L22

And here's the fixture where I create the group chat and delete it afterwards: https://github.com/Deer-Spangle/FA-search-bot/blob/37af664660bd02de32708844a5dbdbc70ad14c7c/fa_search_bot/tests/integration/conftest.py#L63

I've got some tests that check group -> supergroup migration too, so it works fine with both: https://github.com/Deer-Spangle/FA-search-bot/blob/37af664660bd02de32708844a5dbdbc70ad14c7c/fa_search_bot/tests/integration/test_subscriptions.py#L124

hileamlakB commented 3 years ago

Hey there, I use tgIntegration for some of my stuff, and you can actually build tests that use groupchats too. Here's an example of one of my tests using a groupchat, you just pass the group id in as the peer argument to controller.collect(): https://github.com/Deer-Spangle/FA-search-bot/blob/37af664660bd02de32708844a5dbdbc70ad14c7c/fa_search_bot/tests/integration/test_neaten.py#L22

And here's the fixture where I create the group chat and delete it afterwards: https://github.com/Deer-Spangle/FA-search-bot/blob/37af664660bd02de32708844a5dbdbc70ad14c7c/fa_search_bot/tests/integration/conftest.py#L63

I've got some tests that check group -> supergroup migration too, so it works fine with both: https://github.com/Deer-Spangle/FA-search-bot/blob/37af664660bd02de32708844a5dbdbc70ad14c7c/fa_search_bot/tests/integration/test_subscriptions.py#L124

Ok that sounds interesting, I will check it out. Just a quick question, I know it maynot do things the way ptbtest does but can I do something like beloe with tgintergration? you don't have to asnwer it, I will do my research but if you know I would apreciate it.

So I have a unittest to test two of the functionalities of my bot. I setup a mockbot obect, created some mock private, and group chats and then I send the command through both the group and the private chats and see the responses.

class Test_CMDnINS(unittest.TestCase):
    """ Test the CMD and INS funcinality of the game engine object"""

    def __init__(self, *args, **kwargs):

        super(Test_CMDnINS, self).__init__(*args, **kwargs)

        # Prepare the basic mock tools and handlers
        self.bot = Mockbot()
        self.ug = UserGenerator()
        self.cg = ChatGenerator()
        self.mg = MessageGenerator()
        self.updater = Updater(bot=self.bot)
        self.dispatcher = self.updater.dispatcher

        # create the engine to be tested
        self.engin = G91_tgingin()

        # Add the G91_tgngin's engin method as the message handler
        self.dispatcher.add_handler(MessageHandler(
            Filters.text & ~Filters.command, self.engin.engine))

        # add a context
        self.context = CallbackContext(self.dispatcher)

        # Create a user for simulating users
        self.u2 = self.ug.get_user(
            first_name="User", last_name="2", is_bot=False)

        # Create a group chat and idividual chats with the user for chat simulation
        self.gchat = self.cg.get_chat(
            type="supergroup", title="game_91_test_bot", username="g91bot_tester")
        self.u2chat = self.cg.get_chat(user=self.u2)

    def setUp(self):
        self.updater.start_polling()

    def tearDown(self):
        self.updater.stop()

    def test_CMD(self):
        """Tests if the cmd functinoality of the game eninge works well
        in private chat
        """

        # on private chat
        cmd_update = self.mg.get_message(
            text="!CMD", chat=self.u2chat, user=self.u2)
        self.bot.insertUpdate(cmd_update)
        self.assertEqual(self.bot.sent_messages[-1]['text'], cmd_msg)

        # on group chat
        cmd_update = self.mg.get_message(
            text="!CMD", chat=self.gchat, user=self.u2)
        self.bot.insertUpdate(cmd_update)
        self.assertEqual(self.bot.sent_messages[-1]['text'], cmd_msg)
JosXa commented 3 years ago

If you're expecting to run this as strictly local unit tests, I think fixing the ptbtest library will be your best bet. TgIntegration, as the name implies, only affords to create end-to-end system tests that automate conversations by making requests directly to the Telegram servers. See the last paragraph: https://josxa.github.io/tgintegration/

If you're okay to turn your test into an integration test however, you'll find that TgIntegration excels at making very accurate/real-world predictions about the behavior of your bot under test.

GauthamramRavichandran commented 3 years ago

Hi, I had been doing changes/modifications to bring the repo up-to-date. If you want to use/contribute, check here.

Bibo-Joshi commented 2 years ago

Closing in favor of #21