From 57585910ec16b1038d1fe81c96f28fe10b00d50f Mon Sep 17 00:00:00 2001 From: Everaldo Date: Wed, 14 Aug 2024 10:59:25 -0700 Subject: [PATCH] Upgrade scheduled test to send errors. (#111) * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. * Upgrade scheduled test to send errors. --- .github/workflows/scheduled_tests.yml | 103 ++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 5 deletions(-) diff --git a/.github/workflows/scheduled_tests.yml b/.github/workflows/scheduled_tests.yml index ffc653c8..08fd88f6 100644 --- a/.github/workflows/scheduled_tests.yml +++ b/.github/workflows/scheduled_tests.yml @@ -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 @@ -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 @@ -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