Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade scheduled test to send errors. #111

Merged
merged 13 commits into from
Aug 14, 2024
Merged
103 changes: 98 additions & 5 deletions .github/workflows/scheduled_tests.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Scheduled Tests

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

workflow_dispatch: # Manual trigger
schedule: # Scheduled trigger
- cron: "0 18 * * 1-5" # Runs every weekday at 6 AM UTC
jobs:
run_app_tests:
runs-on: ubuntu-latest
Expand All @@ -25,6 +25,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 +39,95 @@ jobs:
- name: Install pytest-slack
run: pip install pytest-slack

- 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"
- name: Create conftest.py for Slack Notifications
uses: DamianReeves/write-file-action@master
with:
path: conftest.py
write-mode: overwrite
contents: |
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": "#logger---web",
"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 send report to Slack
run: python -m pytest src/tests/test_remote.py --slack-webhook-url=${{ secrets.SLACK_WEBHOOK_URL }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

# - name: Setup tmate debug session on failure
# if: ${{ failure() }}
# uses: mxschmitt/action-tmate@v3
Loading