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.<test_name>
- 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.
This commit is contained in:
Misaka_Company
2026-03-27 16:28:00 +08:00
parent e7bbbbc194
commit 440b74d09a
4 changed files with 164 additions and 124 deletions

View File

@@ -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)