Skip to main content

Mocked Telegram Bot API elements for unit tests of a bot based on the pyTelegramBotAPI library.

Project description

Library telemulator3

GitHub Workflow Status GitHub Workflow Status Codacy Badge Codacy Badge

The free, open-source telemulator3 library designed to simplify automatic testing of Telegram bots created using the pyTelegramBotAPI library.

The telemulator3 library partially emulates the Telegram Bot API in unit tests and allows you to create typical scenarios for the interaction of your bot with the Telegram Bot API.

Installation

pip install telemulator3

Usage

import unittest
from telebot import TeleBot
from telemulator3 import Telemulator, send_command

class TestCase(unittest.TestCase, Telemulator):

    def setUp(self):
        """Connect your bot to test suite."""
        super().setUp()
        self.set_tested_bot(TeleBot('xxx-yyy-zzz', threaded=False))

        # Your bot is available via api property.
        # Your need to set bot name and username.
        self.api.bot.username = 'my_bot'
        self.api.bot.name = 'My Bot'

    def test_api(self):
        """Play with API calls."""
        assert not self.api.users

        # create API user for our bot
        bot = self.api.get_me()
        assert bot.is_bot
        assert bot.username == 'my_bot'

        # our bot is a first registered user
        assert len(self.api.users) == 1

        # new user open private chat with bot
        user = self.api.create_user('User')
        chat = user.private()
        send_command(chat, '/start', user)
        assert chat.history.contain('/start')

        # user create group and add bot as member
        group = user.create_group('My group')
        group.add_members(user, [bot])
        assert group.history.contain('invite new members:')

        bot.leave(group)
        assert group.history.contain('My Bot (ID 1) left chat')
        # group.history.dump()

Development

$ git clone git@github.com:vb64/telemulator3
$ cd telemulator3
$ make setup PYTHON_BIN=/path/to/python3
$ make tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telemulator3-1.0.1.tar.gz (20.2 kB view hashes)

Uploaded Source

Built Distribution

telemulator3-1.0.1-py3-none-any.whl (31.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page