Skip to content

Commit

Permalink
Upgrade scheduled test to send errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
everaldorodrigo committed Aug 14, 2024
1 parent b8103cc commit 4578362
Showing 1 changed file with 92 additions and 3 deletions.
95 changes: 92 additions & 3 deletions .github/workflows/scheduled_tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Scheduled Tests

on:
schedule:
- cron: '0 */3 * * *'
workflow_dispatch:
# schedule:
# - cron: '0 */3 * * *'

jobs:
run_app_tests:
Expand All @@ -25,6 +26,11 @@ jobs:
- name: Upgrade pip
run: pip install --upgrade pip

- name: Create python virtual environment
run: |
python -m venv .venv
source .venv/bin/activate
- name: Install dependencies
run: pip install -r requirements_web.txt

Expand All @@ -34,7 +40,90 @@ jobs:
- name: Install pytest-slack
run: pip install pytest-slack

- name: Create conftest.py for Slack Notifications
uses: andstor/create-file-action@v1
with:
path: src/tests/conftest.py
content: |
import pytest
import requests
from _pytest.terminal import TerminalReporter
@pytest.hookimpl(tryfirst=True)
def pytest_terminal_summary(terminalreporter: TerminalReporter, exitstatus: int, config):
"""Customize pytest terminal summary and send to Slack."""
SLACK_WEBHOOK_URL = config.getoption('--slack-webhook-url')
# Collect test summary information
total_tests = terminalreporter.stats.get('passed', []) + \
terminalreporter.stats.get('failed', []) + \
terminalreporter.stats.get('error', []) + \
terminalreporter.stats.get('skipped', [])
passed_tests = len(terminalreporter.stats.get('passed', []))
failed_tests = len(terminalreporter.stats.get('failed', []))
error_tests = len(terminalreporter.stats.get('error', []))
skipped_tests = len(terminalreporter.stats.get('skipped', []))
# Prepare error details
error_details = ""
for test in terminalreporter.stats.get('failed', []) + terminalreporter.stats.get('error', []):
error_details += f"• *Test*: `{test.nodeid}`\n"
error_details += f" _Details:_\n```\n{''.join(test.longreprtext.splitlines(keepends=True)[-10:])}```\n\n"
error_details = "*Error Details:*\n" + error_details if error_details else ""
# Determine status emoji and color
status_emoji = ":thumbsup:" if failed_tests == 0 and error_tests == 0 else ":thumbsdown:"
bug_emoji = "" if failed_tests == 0 and error_tests == 0 else ":bug-happy:"
status_color = "good" if failed_tests == 0 and error_tests == 0 else "danger"
# Check if there's anything to report
if len(total_tests) == 0:
terminalreporter.write("No tests were run, skipping Slack notification.\n")
return
# Create the payload for Slack
slack_data = {
"channel": "#observability-test",
"username": "Mygeneset.Info Tests",
"icon_emoji": f"{status_emoji}",
"attachments": [
{
"color": status_color,
"title": f"{bug_emoji} Mygeneset Pytest Summary",
"text": f"Total Tests: *{len(total_tests)}*\n"
f"Passed: *{passed_tests}* :white_check_mark:\n"
f"Failed: *{failed_tests}* :x:\n"
f"Errors: *{error_tests}* :exclamation:\n"
f"Skipped: *{skipped_tests}* :fast_forward:\n\n"
f"{error_details}"
}
]
}
# Send to Slack
webhook_url = SLACK_WEBHOOK_URL # config.getoption('--slack-webhook-url')
if webhook_url:
response = requests.post(webhook_url, json=slack_data)
if response.status_code == 200:
terminalreporter.write("Slack notification sent successfully.\n")
else:
terminalreporter.write(f"Failed to send message to Slack: {response.status_code}, {response.text}\n")
else:
terminalreporter.write("Slack webhook URL not provided, skipping notification.\n")
@pytest.hookimpl(tryfirst=True)
def pytest_addoption(parser):
"""Add command-line options for Slack integration."""
parser.addoption("--slack-webhook-url", action="store", default=None, help="Slack webhook URL to send messages")
parser.addoption("--slack-channel", action="store", default="#general", help="Slack channel to send messages")
parser.addoption("--slack-username", action="store", default="pytest-bot", help="Slack username to send messages as")
# ### How to run manually:
# python -m pytest src/tests/test_remote.py --slack-webhook-url=$SLACK_WEBHOOK_URL
- name: Run pytest and generate report
run: pytest src/tests/test_remote.py --slack_hook=${{ secrets.SLACK_WEBHOOK_URL }} --slack_channel=observability-test --slack_username="mygeneset.info tests"
run: python -m pytest src/tests/test_remote.py --slack-webhook-url=${{ secrets.SLACK_WEBHOOK_URL }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

0 comments on commit 4578362

Please sign in to comment.