Pytest Messenger
pytest-messenger is a pytest plugin for sending test execution reports to various instant messaging platforms, including Slack, DingTalk, and Telegram. It streamlines communication for CI/CD pipelines and development teams by providing automated notifications of test outcomes. The library is actively maintained, with the current version being 3.3.0, and receives updates periodically.
Warnings
- breaking In version 3.0.0, the package was renamed from `pytest-slack` to `pytest-messenger` to reflect support for multiple messaging platforms (Slack, DingTalk, Telegram). If upgrading from an older version (pre-3.0.0) that used `pytest-slack`, you must uninstall the old package and install `pytest-messenger`. Command-line arguments and configuration options might also have minor changes.
- gotcha Sensitive information like webhook URLs and API tokens should not be hardcoded in your test files or version-controlled configuration files. Always use environment variables, a secrets management system, or a `pytest.ini` file with appropriate access controls.
- gotcha If reports are not being sent, check that the provided webhook URL or API token (for Slack, DingTalk, Telegram) is correct and has the necessary permissions in your messenger application. Incorrect credentials often lead to silent failures where no report is posted.
Install
-
pip install pytest-messenger
Quickstart
import os
import pytest
# To simulate a test run and trigger reporting
def test_example_success():
assert True
def test_example_failure():
assert False
# To run this example:
# 1. Create a virtual environment and install pytest-messenger:
# pip install pytest-messenger pytest
# 2. Set your Slack webhook URL as an environment variable:
# export SLACK_WEBHOOK_URL='https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
# 3. (Optional) Set a Slack channel and username:
# export SLACK_CHANNEL='#test-reports'
# export SLACK_USERNAME='CI Bot'
# 4. Save the above Python code as a test file (e.g., test_report.py).
# 5. Run pytest with the plugin options (values are pulled from env vars):
# pytest test_report.py \
# --slack_hook="${SLACK_WEBHOOK_URL}" \
# --slack_channel="${SLACK_CHANNEL:-#general}" \
# --slack_username="${SLACK_USERNAME:-Pytest Report}"