Pytest Slack Reporting Plugin
pytest-slack is a pytest plugin designed to send test execution reports directly to Slack. While it provided basic reporting, development on this package ceased after version 2.3.1 (December 2020). The project has since been renamed and continued as `pytest-messenger`, which offers broader messaging support including Slack, DingTalk, and Telegram. Users seeking active development and new features should use `pytest-messenger` instead.
Common errors
-
Tests run but no report sent to Slack.
cause The configured Slack webhook URL is incorrect, inaccessible, or there's a network issue preventing the plugin from reaching Slack's API.fixVerify the `--slack_hook` value for typos, ensure it's a valid Slack Incoming Webhook URL, and check network connectivity from the environment where `pytest` is executed. Also, ensure the Slack channel exists and the webhook has permissions to post to it. -
ERROR: [Errno 111] Connection refused (or similar network error)
cause The system running `pytest-slack` cannot establish a network connection to the Slack API endpoint, possibly due to firewall rules, proxy issues, or a temporary outage.fixCheck local firewall settings, ensure any necessary proxy configurations are correctly set in the environment, and confirm that Slack's API is reachable (e.g., `curl -v <slack_webhook_url>`). -
Received multiple messages on the Slack channel when running tests with `pytest-xdist`.
cause The `pytest_sessionfinish` hook, which might be implicitly or explicitly used for reporting, is executed by each parallel worker process of `pytest-xdist`.fixImplement reporting logic within the `pytest_terminal_summary` hook in your `conftest.py`. This hook runs only once after all test workers have completed their execution, allowing for a single aggregated report.
Warnings
- breaking The `pytest-slack` project has been renamed to `pytest-messenger` starting from version 3.0.0 (on GitHub). This package (`pytest-slack`) is no longer actively maintained, with its last PyPI release (2.3.1) in December 2020. Users should migrate to `pytest-messenger` for continued development and new features.
- gotcha Passing Slack webhook URLs directly on the command line (`--slack_hook`) can expose sensitive credentials in logs or command history. This is particularly risky in CI/CD pipelines.
- gotcha When running tests in parallel with `pytest-xdist`, using the `pytest_sessionfinish` hook in `conftest.py` for Slack reporting can result in multiple messages being sent (one from each worker process).
Install
-
pip install pytest-slack
Quickstart
# Save this in a test file (e.g., test_example.py)
def test_passing():
assert True
def test_failing():
assert False
# To run and report to Slack:
# Set your Slack webhook URL as an environment variable or pass directly
# Example using environment variable:
# export SLACK_HOOK='https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX'
# pytest --slack_hook=$SLACK_HOOK --slack_channel='#test-reports' --slack_username='Pytest Bot'
# For local testing without environment variables (less secure for CI/CD):
# pytest --slack_hook='https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX' --slack_channel='#test-reports' --slack_username='Pytest Bot'