🔍 Code Extractor

function main_v42

Maturity: 44

Orchestrates and executes a test suite for an email forwarder service, running multiple test functions sequentially and reporting results.

File:
/tf/active/vicechatdev/email-forwarder/test_service.py
Lines:
96 - 130
Complexity:
simple

Purpose

This function serves as the main entry point for testing an email forwarding service. It sets up logging, executes a predefined list of test functions (configuration, O365 connection, email handler, and send email tests), tracks pass/fail status, and provides a summary report. Returns 0 for success (all tests passed) or 1 for failure (some tests failed).

Source Code

def main():
    """Run all tests."""
    print("Email Forwarder Service Test Suite")
    print("=" * 40)
    
    # Setup logging
    setup_logging()
    
    tests = [
        test_configuration,
        test_o365_connection,
        test_email_handler,
        test_send_email
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"✗ Test failed with exception: {e}")
        print()
    
    print("=" * 40)
    print(f"Tests passed: {passed}/{total}")
    
    if passed == total:
        print("All tests passed! ✓")
        return 0
    else:
        print("Some tests failed! ✗")
        return 1

Return Value

Returns an integer exit code: 0 if all tests passed successfully, 1 if any tests failed. This follows standard Unix convention for process exit codes where 0 indicates success.

Dependencies

  • logging
  • sys
  • os
  • pathlib

Required Imports

import sys
import os
import logging
from pathlib import Path
from config import settings
from utils.logger import setup_logging
from forwarder.o365_client import O365Client
from forwarder.email_handler import EmailHandler

Usage Example

# Assuming all required modules and test functions are defined
# in the same file or imported

def test_configuration():
    """Example test function."""
    return True

def test_o365_connection():
    """Example test function."""
    return True

def test_email_handler():
    """Example test function."""
    return True

def test_send_email():
    """Example test function."""
    return True

if __name__ == '__main__':
    exit_code = main()
    sys.exit(exit_code)

Best Practices

  • This function expects test functions to be defined in the same module scope before calling main()
  • Each test function should return a boolean (True for pass, False for fail) or raise an exception
  • The function uses print statements for output; consider redirecting stdout if capturing test results programmatically
  • Exit codes follow Unix convention: use sys.exit(main()) to properly terminate the process with the correct code
  • Test functions are executed sequentially; a failed test does not stop subsequent tests from running
  • Exceptions in test functions are caught and counted as failures, allowing the test suite to continue
  • The setup_logging() function is called before tests run; ensure logging configuration is appropriate for test environment

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function run_all_tests 73.5% similar

    Orchestrates a comprehensive test suite for the Vendor Email Extractor system, verifying configuration, authentication, mailbox access, email search, and LLM connectivity.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function main_v69 71.3% similar

    Command-line interface function that parses arguments and sends a test email through an SMTP forwarder service, displaying connection details and returning an exit code based on success.

    From: /tf/active/vicechatdev/email-forwarder/send_test_email.py
  • function main_v25 70.3% similar

    Orchestrates and executes a comprehensive test suite for the Vice AI Data Analysis Integration, running multiple test functions, creating test datasets, and providing detailed pass/fail reporting.

    From: /tf/active/vicechatdev/vice_ai/test_integration.py
  • function main_v40 70.3% similar

    Test orchestration function that executes a comprehensive test suite for DocChat's multi-LLM model selection feature and reports results.

    From: /tf/active/vicechatdev/docchat/test_model_selection.py
  • function main_v19 69.8% similar

    Orchestrates and executes a comprehensive test suite for SharePoint to FileCloud synchronization service, running configuration, connection, and operation tests.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
← Back to Browse