From 440b74d09aea0b822fc8df60aff5fb32f3384b5b Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Fri, 27 Mar 2026 16:28:00 +0800 Subject: [PATCH] test(logging): unify test file logging to standard logging module - Replace print() statements with logging module in all test files - Use consistent logger naming: bipauto.tests. - Apply unified log format: [%(levelname)s] %(name)s: %(message)s - Migrates test_extractor_real.py, test_login.py, test_auth_config.py, test_extractor_component.py This change ensures consistent log output format between test files and utils modules, matching the existing bipauto.* logger hierarchy. --- tests/test_auth_config.py | 44 +++++++----- tests/test_extractor_component.py | 78 ++++++++++++--------- tests/test_extractor_real.py | 112 ++++++++++++++++-------------- tests/test_login.py | 54 ++++++++------ 4 files changed, 164 insertions(+), 124 deletions(-) diff --git a/tests/test_auth_config.py b/tests/test_auth_config.py index 1e67e5f..2a2fa5f 100644 --- a/tests/test_auth_config.py +++ b/tests/test_auth_config.py @@ -2,6 +2,7 @@ Test if auth module configuration is correct """ +import logging from playwright.sync_api import sync_playwright from dotenv import load_dotenv from pathlib import Path @@ -17,31 +18,40 @@ if not browser_path: raise ValueError("PLAYWRIGHT_BROWSERS_PATH environment variable is required") os.environ["PLAYWRIGHT_BROWSERS_PATH"] = browser_path -print("=" * 50) -print("ERP System Configuration Check") -print("=" * 50) +# Setup logging +logger = logging.getLogger('bipauto.tests.auth_config') +logger.setLevel(logging.INFO) +if not logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter("[%(levelname)s] %(name)s: %(message)s")) + logger.addHandler(handler) +logger.propagate = False + +logger.info("=" * 50) +logger.info("ERP System Configuration Check") +logger.info("=" * 50) # Display configuration information -print(f"Browser Path: {os.getenv('PLAYWRIGHT_BROWSERS_PATH')}") -print(f"ERP URL: {os.getenv('ERP_URL')}") -print(f"Username: {os.getenv('ERP_USERNAME')}") -print(f"Password: {'*' * len(os.getenv('ERP_PASSWORD', ''))}") -print(f"Headless Mode: {os.getenv('ERP_HEADLESS')}") -print(f"Ignore HTTPS Errors: {os.getenv('ERP_IGNORE_HTTPS_ERRORS')}") -print(f"Auto Close Browser: {os.getenv('ERP_AUTO_CLOSE_BROWSER')}") +logger.info(f"Browser Path: {os.getenv('PLAYWRIGHT_BROWSERS_PATH')}") +logger.info(f"ERP URL: {os.getenv('ERP_URL')}") +logger.info(f"Username: {os.getenv('ERP_USERNAME')}") +logger.info(f"Password: {'*' * len(os.getenv('ERP_PASSWORD', ''))}") +logger.info(f"Headless Mode: {os.getenv('ERP_HEADLESS')}") +logger.info(f"Ignore HTTPS Errors: {os.getenv('ERP_IGNORE_HTTPS_ERRORS')}") +logger.info(f"Auto Close Browser: {os.getenv('ERP_AUTO_CLOSE_BROWSER')}") -print("\n" + "=" * 50) -print("Check Playwright Browser") -print("=" * 50) +logger.info("=" * 50) +logger.info("Check Playwright Browser") +logger.info("=" * 50) with sync_playwright() as p: chromium_path = p.chromium.executable_path - print(f"Chromium Path: {chromium_path}") + logger.info(f"Chromium Path: {chromium_path}") # Check if browser file exists if os.path.exists(chromium_path): - print("[OK] Browser file exists") + logger.info("Browser file exists") else: - print("[ERROR] Browser file not found") + logger.error("Browser file not found") -print("\n[OK] Configuration check completed!") +logger.info("Configuration check completed!") diff --git a/tests/test_extractor_component.py b/tests/test_extractor_component.py index e034ee5..d4732b2 100644 --- a/tests/test_extractor_component.py +++ b/tests/test_extractor_component.py @@ -6,6 +6,7 @@ Does not require actual browser or ERP connection. """ import sys +import logging from pathlib import Path # Add project root to Python path @@ -20,13 +21,22 @@ load_dotenv(PROJECT_ROOT / ".env") import os os.environ["PLAYWRIGHT_BROWSERS_PATH"] = os.getenv("PLAYWRIGHT_BROWSERS_PATH", "") -print("=" * 60) -print("Testing discrete_material_plan.extractor Functions") -print("=" * 60) +# Setup logging +logger = logging.getLogger('bipauto.tests.extractor_component') +logger.setLevel(logging.INFO) +if not logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter("[%(levelname)s] %(name)s: %(message)s")) + logger.addHandler(handler) +logger.propagate = False + +logger.info("=" * 60) +logger.info("Testing discrete_material_plan.extractor Functions") +logger.info("=" * 60) try: # Test 1: Import all functions - print("\n[1/5] Importing extractor functions...") + logger.info("Importing extractor functions...") from utils.discrete_material_plan.extractor import ( chunk_order_ids, get_login_url, @@ -36,61 +46,61 @@ try: read_order_ids_from_file, extract_from_file, ) - print("[OK] All functions imported successfully") + logger.info("All functions imported successfully") # Test 2: Test chunk_order_ids - print("\n[2/5] Testing chunk_order_ids...") + logger.info("Testing chunk_order_ids...") # Test normal chunking result = chunk_order_ids(["A", "B", "C", "D", "E"], 2) expected = [["A", "B"], ["C", "D"], ["E"]] assert result == expected, f"Expected {expected}, got {result}" - print(f" chunk_order_ids(['A','B','C','D','E'], 2) = {result}") + logger.info(f" chunk_order_ids(['A','B','C','D','E'], 2) = {result}") # Test exact division result = chunk_order_ids(["A", "B", "C", "D"], 2) expected = [["A", "B"], ["C", "D"]] assert result == expected, f"Expected {expected}, got {result}" - print(f" chunk_order_ids(['A','B','C','D'], 2) = {result}") + logger.info(f" chunk_order_ids(['A','B','C','D'], 2) = {result}") # Test batch size larger than list result = chunk_order_ids(["A", "B"], 10) expected = [["A", "B"]] assert result == expected, f"Expected {expected}, got {result}" - print(f" chunk_order_ids(['A','B'], 10) = {result}") + logger.info(f" chunk_order_ids(['A','B'], 10) = {result}") # Test empty list result = chunk_order_ids([], 5) expected = [] assert result == expected, f"Expected {expected}, got {result}" - print(f" chunk_order_ids([], 5) = {result}") + logger.info(f" chunk_order_ids([], 5) = {result}") - print("[OK] chunk_order_ids works correctly") + logger.info("chunk_order_ids works correctly") # Test 3: Test get_login_url - print("\n[3/5] Testing get_login_url...") + logger.info("Testing get_login_url...") # Test with trailing slash result = get_login_url("https://erp.example.com/") expected = "https://erp.example.com/yonbip/resources/uap/rbac/login/main/index.html" assert result == expected, f"Expected {expected}, got {result}" - print(f" get_login_url('https://erp.example.com/') = {result}") + logger.info(f" get_login_url('https://erp.example.com/') = {result}") # Test without trailing slash result = get_login_url("https://erp.example.com") assert result == expected, f"Expected {expected}, got {result}" - print(f" get_login_url('https://erp.example.com') = {result}") + logger.info(f" get_login_url('https://erp.example.com') = {result}") # Test with path result = get_login_url("https://erp.example.com/some/path/") expected = "https://erp.example.com/some/path/yonbip/resources/uap/rbac/login/main/index.html" assert result == expected, f"Expected {expected}, got {result}" - print(f" get_login_url('https://erp.example.com/some/path/') = {result}") + logger.info(f" get_login_url('https://erp.example.com/some/path/') = {result}") - print("[OK] get_login_url works correctly") + logger.info("get_login_url works correctly") # Test 4: Test read_order_ids_from_file - print("\n[4/5] Testing read_order_ids_from_file...") + logger.info("Testing read_order_ids_from_file...") # Create a temporary test file import tempfile @@ -107,8 +117,8 @@ try: result = read_order_ids_from_file(temp_file) expected = ["ID001", "ID002", "ID003", "ID004"] assert result == expected, f"Expected {expected}, got {result}" - print(f" read_order_ids_from_file(temp_file) = {result}") - print("[OK] read_order_ids_from_file works correctly") + logger.info(f" read_order_ids_from_file(temp_file) = {result}") + logger.info("read_order_ids_from_file works correctly") finally: # Cleanup temp file Path(temp_file).unlink() @@ -116,13 +126,13 @@ try: # Test FileNotFoundError try: read_order_ids_from_file("nonexistent_file.txt") - print("[FAIL] Should have raised FileNotFoundError") + logger.error("Should have raised FileNotFoundError") raise AssertionError("Should have raised FileNotFoundError") except FileNotFoundError: - print(f" read_order_ids_from_file('nonexistent') raises FileNotFoundError [OK]") + logger.info(" read_order_ids_from_file('nonexistent') raises FileNotFoundError") # Test 5: Test module exports - print("\n[5/5] Testing module exports...") + logger.info("Testing module exports...") from utils.discrete_material_plan import ( chunk_order_ids as exported_chunk, get_login_url as exported_get_url, @@ -132,22 +142,22 @@ try: read_order_ids_from_file as exported_read_ids, extract_from_file as exported_extract_file, ) - print("[OK] All functions exported correctly from module") + logger.info("All functions exported correctly from module") - print("\n" + "=" * 60) - print("[SUCCESS] All extractor function tests passed!") - print("=" * 60) - print("\nNote: extract_batch, extract_batches, extract_and_post_process,") - print(" and extract_from_file require actual browser session and") - print(" are not tested here. Integration tests cover those cases.") + logger.info("=" * 60) + logger.info("ALL EXTRACTOR FUNCTION TESTS PASSED") + logger.info("=" * 60) + logger.info("Note: extract_batch, extract_batches, extract_and_post_process,") + logger.info(" and extract_from_file require actual browser session and") + logger.info(" are not tested here. Integration tests cover those cases.") except Exception as e: - print(f"\n[ERROR] Function test failed!") - print(f"Error: {e}") + logger.error(f"Function test failed!") + logger.error(f"Error: {e}") import traceback traceback.print_exc() sys.exit(1) -print("\n" + "=" * 60) -print("Test completed successfully") -print("=" * 60) +logger.info("=" * 60) +logger.info("Test completed successfully") +logger.info("=" * 60) diff --git a/tests/test_extractor_real.py b/tests/test_extractor_real.py index e4520fd..5491c84 100644 --- a/tests/test_extractor_real.py +++ b/tests/test_extractor_real.py @@ -12,6 +12,7 @@ This test: """ import sys +import logging from pathlib import Path from datetime import datetime @@ -27,6 +28,15 @@ load_dotenv(PROJECT_ROOT / ".env") import os os.environ["PLAYWRIGHT_BROWSERS_PATH"] = os.getenv("PLAYWRIGHT_BROWSERS_PATH", "") +# Setup logging +logger = logging.getLogger('bipauto.tests.extractor_real') +logger.setLevel(logging.INFO) +if not logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter("[%(levelname)s] %(name)s: %(message)s")) + logger.addHandler(handler) +logger.propagate = False + # Generate timestamp for unique output files TIMESTAMP = datetime.now().strftime("%Y%m%d_%H%M%S") @@ -39,32 +49,32 @@ OUTPUT_FILE = OUTPUT_DIR / f"merged_result_{TIMESTAMP}.xlsx" DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True) OUTPUT_DIR.mkdir(parents=True, exist_ok=True) -print("=" * 60) -print("Discrete Material Plan Extractor - Integration Test") -print("=" * 60) -print(f"\nConfiguration:") -print(f" ID File: {PROJECT_ROOT / 'tests' / 'id-demo.txt'}") -print(f" Download Dir: {DOWNLOAD_DIR}") -print(f" Output File: {OUTPUT_FILE}") -print(f" ERP URL: {os.getenv('ERP_URL', 'Not set')}") -print(f" Headless: {os.getenv('ERP_HEADLESS', 'true')}") -print("=" * 60) +logger.info("=" * 60) +logger.info("Discrete Material Plan Extractor - Integration Test") +logger.info("=" * 60) +logger.info(f"Configuration:") +logger.info(f" ID File: {PROJECT_ROOT / 'tests' / 'id-demo.txt'}") +logger.info(f" Download Dir: {DOWNLOAD_DIR}") +logger.info(f" Output File: {OUTPUT_FILE}") +logger.info(f" ERP URL: {os.getenv('ERP_URL', 'Not set')}") +logger.info(f" Headless: {os.getenv('ERP_HEADLESS', 'true')}") +logger.info("=" * 60) # Verify environment variables required_vars = ["ERP_USERNAME", "ERP_PASSWORD", "ERP_URL"] missing_vars = [var for var in required_vars if not os.getenv(var)] if missing_vars: - print(f"\n[ERROR] Missing required environment variables: {missing_vars}") - print("Please ensure .env file contains:") - print(" ERP_USERNAME=your_username") - print(" ERP_PASSWORD=your_password") - print(" ERP_URL=https://your-erp-url.com") + logger.error(f"Missing required environment variables: {missing_vars}") + logger.error("Please ensure .env file contains:") + logger.error(" ERP_USERNAME=your_username") + logger.error(" ERP_PASSWORD=your_password") + logger.error(" ERP_URL=https://your-erp-url.com") sys.exit(1) try: # Import required modules - print("\n[1/6] Importing modules...") + logger.info("[1/6] Importing modules...") from playwright.sync_api import sync_playwright from utils.auth import login from utils.discrete_material_plan import ( @@ -73,18 +83,18 @@ try: read_order_ids_from_file, get_login_url, ) - print("[OK] Modules imported successfully") + logger.info("Modules imported successfully") # Read order IDs from demo file - print("\n[2/6] Reading order IDs from data/demo/id-demo.txt...") + logger.info("[2/6] Reading order IDs from data/demo/id-demo.txt...") id_file = PROJECT_ROOT / "data" / "demo" / "id-demo.txt" order_ids = read_order_ids_from_file(str(id_file)) - print(f"[OK] Loaded {len(order_ids)} order IDs") + logger.info(f"Loaded {len(order_ids)} order IDs") test_order_ids = order_ids # Setup browser and extract data - print("\n[3/6] Launching browser and logging in...") + logger.info("[3/6] Launching browser and logging in...") with sync_playwright() as playwright: # Get required environment variables (with validation) username = os.getenv("ERP_USERNAME") @@ -107,17 +117,17 @@ try: try: # Navigate to discrete material page - print("\n[4/6] Navigating to discrete material plan page...") + logger.info("[4/6] Navigating to discrete material plan page...") work_frame, page1 = navigate_to_discrete_material_page(main_frame, page) - print("[OK] Navigation successful") + logger.info("Navigation successful") # Extract and post-process - print("\n[5/6] Extracting data and post-processing...") - print(f" Orders: {len(test_order_ids)}") - print(f" Batch size: 100") - print(f" Download dir: {DOWNLOAD_DIR}") - print(f" Output file: {OUTPUT_FILE}") - print(f" Cleanup temp files: True (default)") + logger.info("[5/6] Extracting data and post-processing...") + logger.info(f" Orders: {len(test_order_ids)}") + logger.info(f" Batch size: 100") + logger.info(f" Download dir: {DOWNLOAD_DIR}") + logger.info(f" Output file: {OUTPUT_FILE}") + logger.info(f" Cleanup temp files: True (default)") output_path, df = extract_and_post_process( work_frame=work_frame, @@ -130,45 +140,45 @@ try: cleanup_temp_files=True, # Default: True, set to False to keep temp files ) - print("\n[6/6] Verifying results...") - print(f"[OK] Extraction completed successfully") - print(f" Output file: {output_path}") - print(f" Total records: {len(df)}") - print(f" Columns: {list(df.columns)}") + logger.info("[6/6] Verifying results...") + logger.info("Extraction completed successfully") + logger.info(f" Output file: {output_path}") + logger.info(f" Total records: {len(df)}") + logger.info(f" Columns: {list(df.columns)}") # Verify output file exists if not Path(output_path).exists(): raise FileNotFoundError(f"Output file not found: {output_path}") - print(f"[OK] Output file verified: {output_path}") + logger.info(f"Output file verified: {output_path}") # Show sample data - print("\n" + "=" * 60) - print("Sample Data (first 3 rows, first 5 columns):") - print("=" * 60) + logger.info("=" * 60) + logger.info("Sample Data (first 3 rows, first 5 columns):") + logger.info("=" * 60) # Use unicode encoding for display sample = df.head(3).iloc[:, :5] - print(sample.to_string()) - print("\n[OK] Data extraction verified - Chinese characters displayed correctly in console encoding") + logger.info("\n" + sample.to_string()) + logger.info("Data extraction verified - Chinese characters displayed correctly in console encoding") finally: # Cleanup browser - print("\n\n[Cleanup] Closing browser...") + logger.info("Closing browser...") context.close() browser.close() - print("[OK] Browser closed") + logger.info("Browser closed") - print("\n" + "=" * 60) - print("[SUCCESS] Integration test completed successfully!") - print("=" * 60) - print(f"\nOutputs:") - print(f" Raw Excel files: {DOWNLOAD_DIR}/*.xlsx") - print(f" Merged result: {OUTPUT_FILE}") - print(f" Total records: {len(df)}") - print("=" * 60) + logger.info("=" * 60) + logger.info("INTEGRATION TEST COMPLETED SUCCESSFULLY") + logger.info("=" * 60) + logger.info(f"Outputs:") + logger.info(f" Raw Excel files: {DOWNLOAD_DIR}/*.xlsx") + logger.info(f" Merged result: {OUTPUT_FILE}") + logger.info(f" Total records: {len(df)}") + logger.info("=" * 60) except Exception as e: - print(f"\n[ERROR] Integration test failed!") - print(f"Error: {e}") + logger.error(f"Integration test failed!") + logger.error(f"Error: {e}") import traceback traceback.print_exc() sys.exit(1) diff --git a/tests/test_login.py b/tests/test_login.py index 2e1515c..4edfafa 100644 --- a/tests/test_login.py +++ b/tests/test_login.py @@ -3,6 +3,7 @@ Test Yonyou BIP system login functionality """ import sys +import logging from pathlib import Path # Add project root to Python path @@ -23,16 +24,25 @@ if not browser_path: raise ValueError("PLAYWRIGHT_BROWSERS_PATH environment variable is required") os.environ["PLAYWRIGHT_BROWSERS_PATH"] = browser_path -print("=" * 60) -print("Testing Yonyou BIP Login and Logout") -print("=" * 60) +# Setup logging +logger = logging.getLogger('bipauto.tests.login') +logger.setLevel(logging.INFO) +if not logger.handlers: + handler = logging.StreamHandler() + handler.setFormatter(logging.Formatter("[%(levelname)s] %(name)s: %(message)s")) + logger.addHandler(handler) +logger.propagate = False + +logger.info("=" * 60) +logger.info("Testing Yonyou BIP Login and Logout") +logger.info("=" * 60) # Display configuration information -print(f"\nConfiguration:") -print(f" URL: {os.getenv('ERP_URL')}") -print(f" Username: {os.getenv('ERP_USERNAME')}") -print(f" Headless: {os.getenv('ERP_HEADLESS')}") -print(f" Ignore HTTPS Errors: {os.getenv('ERP_IGNORE_HTTPS_ERRORS')}") +logger.info("Configuration:") +logger.info(f" URL: {os.getenv('ERP_URL')}") +logger.info(f" Username: {os.getenv('ERP_USERNAME')}") +logger.info(f" Headless: {os.getenv('ERP_HEADLESS')}") +logger.info(f" Ignore HTTPS Errors: {os.getenv('ERP_IGNORE_HTTPS_ERRORS')}") # Construct complete login URL base_url = os.getenv('ERP_URL') @@ -51,7 +61,7 @@ try: raise ValueError("ERP_USERNAME and ERP_PASSWORD environment variables are required") with sync_playwright() as p: - print("\n[1/5] Starting browser...") + logger.info("[1/5] Starting browser...") browser, context, page, main_frame = login( playwright=p, username=username, @@ -62,42 +72,42 @@ try: verbose=True ) - print("\n[2/5] Login successful!") + logger.info("Login successful!") # Wait a moment to observe the page after login - print("\n[3/5] Waiting 3 seconds to observe the page after login...") + logger.info("[3/5] Waiting 3 seconds to observe the page after login...") import time time.sleep(3) # Test logout - print("\n[4/5] Testing logout...") + logger.info("[4/5] Testing logout...") logout(main_frame, verbose=True) - print("\n[5/5] Logout successful!") + logger.info("Logout successful!") # Wait a moment to observe the page after logout - print("\nWaiting 2 seconds to observe the page after logout...") + logger.info("Waiting 2 seconds to observe the page after logout...") time.sleep(2) - print("\n[SUCCESS] Login and logout test completed successfully!") + logger.info("Login and logout test completed successfully!") # Check if auto-close browser auto_close = os.getenv("ERP_AUTO_CLOSE_BROWSER", "true").lower() in ("true", "1", "yes") if not auto_close: - print("\n[INFO] Browser will stay open for 10 seconds for manual observation...") + logger.info("Browser will stay open for 10 seconds for manual observation...") time.sleep(10) # Close browser - print("\n[CLEANUP] Closing browser session...") + logger.info("Closing browser session...") context.close() browser.close() except Exception as e: - print(f"\n[ERROR] Login/logout test failed!") - print(f"Error: {e}") + logger.error(f"Login/logout test failed!") + logger.error(f"Error: {e}") import traceback traceback.print_exc() -print("\n" + "=" * 60) -print("Test completed") -print("=" * 60) +logger.info("=" * 60) +logger.info("Test completed") +logger.info("=" * 60)