How To Set Up Telegram Reporting For Pytest

Step-by-step instructions for Telegram API integration with Pytest

Dmitrii Bormotov
Python Pandemonium
4 min readMar 18, 2021

--

Photo by freestocks on Unsplash

Ahoy, mate!

Today you’ll learn how to set up reporting to your Telegram channel with your testing framework if you use pytest.

Why would you ever need it? Well, some companies use Telegram as a corporate messenger, so they have group chats and channels for various tasks (development teams, QA, general chat and et cetera). There’s a thing that such companies would appreciate for sure — getting information about recent test runs in one of their channels (maybe even a separate one).

The same kind of reports is possible for most of the messengers that have a public API. The old-style method to inform development team about test run finish is an email (and some companies still use this method).

Plan

Let’s define a step-by-step plan for our today’s task:

  1. Telegram setup.
  2. Install dependencies.
  3. Prepare and send report.

This is a logic division for minimal report messages in Telegram. Following these steps, you’ll be able to send a summary report about test run to your Telegram channel/group. You may customize these steps for your needs in future.

Telegram setup

Create bot

First, what you need to do, is to create a Telegram bot, since you’ll need it’s token to send messages to channels/groups where you’ll include this bot.

BotFather bot in Telegram

Look for the BotFather bot in Telegram and send him /start message, in response he will list all possible commands and their description.

Dialog start with BotFather

What we need is /newbot command — simply click it or type it in your chat with BotFather and then follow the instructions that he provides. At the end, BotFather will send you token to access the HTTP API — we’ll need it later in our code, so keep it somewhere close.

Create channel

Now we need to create a new group or channel (whatever you like) and add bot that we’ve just created as an admin.

Get your Chat ID

When bot is in the room, you have to send a message to this channel (from your personal Telegram) to have logs in your bot so we could identify that room (group/channel).

Then, you have to visit this URL (don’t forget to replace <BOT_TOKEN> with your actual Telegram bot token):

https://api.telegram.org/bot<BOT_TOKEN>/getUpdates

You’ll get a message like this — your chat_id is an ID from sender_chat object (I’ve marked it blue on picture).

Response from Telegram API

Install dependencies

Since we’re working on project that uses pytest, then we already have Python and pytest. We need to install only one thing — the package that helps us to interact with Telegram API, it’s pyTelegramBotAPI:

pip install pyTelegramBotAPI

Prepare and send report

First thing that we need to do in our code is to gather test session logs:

  • tests failed / total
  • failed tests names

We can do it in pytest_sessionfinish method, that has session argument, that contains entire test session data, which is just what we need…

Required imports here are pytest, ExitCode from pytest and telebot. Then, we call pytest_sessionfinish method with two arguments: session and exitstatus. First contains whole session data and second contains session’s exit status (which tells us if tests have passed or failed).

In body of this method, we check if tests have failed, and if so, we continue with our report (and if not — we don’t report anything). We iterate through tests in session and check if they’ve failed, and if so, we get their names and store them in list.

Finally, we prepare a message with gathered data and send via Telegram API. You’ll need to provide your Telegram bot token to TeleBot and chat_id for your Telegram channel/group to send_message method.

You can see an example code below (gist):

After your test run, if you have any failed tests, you’ll get a report like this:

Message about test run fails in Telegram

This message serves as an alert that something went wrong during your CI/CD process or simply test run and you have to take a look at it.

Thanks for reading, I hope you’ve learned something new today!

--

--