=============================================================================== Testing =============================================================================== This project includes comprehensive test coverage with support for parallel execution. Test Structure =============================================================================== The project includes comprehensive test coverage: .. code-block:: text tests/ ├── functional/ # Functional tests (deploy to AWS) │ ├── test_lambda_creation.py │ └── test_sns_sqs_lambda_s3_integration.py └── unit/ # Unit tests (fast, isolated) .. Running Tests =============================================================================== Sequential Execution --------------------------------------- Run all tests: .. code-block:: shell python manager.py run-tests python manager.py run-tests --test-type integration python manager.py run-coverage # Having proper AWS credentials... python manager.py run-tests --test-type functional --pattern "*.py" # Or using pytest... pytest .. Run specific test file: .. code-block:: shell pytest tests/functional/test_lambda_creation.py .. Run specific test: .. code-block:: shell pytest tests/functional/test_lambda_creation.py::TestLambdaCreation::test_create_and_invoke_lambda_with_inline_code .. Parallel Execution --------------------------------------- Install pytest-xdist: .. code-block:: shell pip install pytest-xdist # or pip install -e ".[dev]" .. Run tests in parallel using all CPUs: .. code-block:: shell pytest -n auto .. Run with specific number of workers: .. code-block:: shell pytest -n 4 # Use 4 parallel workers .. Run functional tests with limited parallelism (recommended): .. code-block:: shell pytest tests/functional/ -n 2 # Avoid AWS rate limits .. Test Markers =============================================================================== Filter tests by markers: .. code-block:: shell # Run only unit tests pytest -m unit # Run only functional tests pytest -m functional # Run only integration tests pytest -m integration # Exclude slow tests pytest -m "not slow" .. Available markers: * ``unit`` - Unit tests (fast, no external dependencies) * ``functional`` - Functional tests (deploy to AWS, slower) * ``integration`` - Integration tests (multiple services) * ``slow`` - Slow running tests Functional Tests =============================================================================== **IMPORTANT:** Functional tests deploy real resources to AWS and may incur costs. Prerequisites --------------------------------------- 1. AWS credentials configured: .. code-block:: shell aws configure .. 2. CDK CLI installed: .. code-block:: shell npm install -g aws-cdk .. 3. Required AWS permissions: * Lambda (create, invoke, delete) * S3 (create bucket, put/get objects, delete) * SNS (create topic, publish) * SQS (create queue, send/receive messages) * CloudFormation (create/update/delete stacks) * IAM (create roles and policies) Running Functional Tests --------------------------------------- Sequential (safer for AWS rate limits): .. code-block:: shell pytest tests/functional/ -v -s .. Parallel (faster but may hit rate limits): .. code-block:: shell pytest tests/functional/ -n 2 -v -s .. Important Notes: * Tests automatically clean up resources after completion * Each test uses temporary directories and unique resource names * Tests include 10-minute timeouts for deployment and cleanup * All logs captured with DEBUG level for troubleshooting Test Output Options =============================================================================== Verbose output: .. code-block:: shell pytest -v # Show test names pytest -vv # Show more details .. Show print statements: .. code-block:: shell pytest -s # Show stdout/stderr and logger output .. Combine options: .. code-block:: shell pytest tests/functional/ -n auto -v -s .. Debugging =============================================================================== Show full traceback: .. code-block:: shell pytest --tb=long .. Run only failed tests: .. code-block:: shell pytest --lf # Last failed pytest --ff # Failed first .. Stop on first failure: .. code-block:: shell pytest -x .. Drop into debugger on failure: .. code-block:: shell pytest --pdb .. Code Coverage =============================================================================== Generate coverage report: .. code-block:: shell pytest --cov=core_aws_cdk --cov-report=html .. Run with coverage in parallel: .. code-block:: shell pytest -n auto --cov=core_aws_cdk --cov-report=html .. View HTML report: .. code-block:: shell open htmlcov/index.html .. Testing Lambda Packaging =============================================================================== Before deploying Lambda functions, you can test the ZIP packaging locally to verify that all dependencies and files are correctly bundled. Basic Package Testing --------------------------------------- Create a test script to verify Lambda package creation: .. code-block:: python # test.py from core_aws_cdk.stacks.lambdas import ZipAssetCode import pathlib result = ZipAssetCode( project_directory=pathlib.Path("/path/to/project"), work_dir=pathlib.Path("/path/to/lambda"), include_project_folders=["commons"], includes=["__init__.py", "handler.py", "docs"], debug=True ) print(result.package_path.resolve()) .. Run the test script: .. code-block:: shell python test.py .. Expected output: .. code-block:: text /path/to/project/.build/lambda_XXXXX.zip .. Verifying Package Contents --------------------------------------- Inspect the generated ZIP file: .. code-block:: shell unzip -l /path/to/project/.build/lambda_XXXXX.zip .. The ZIP should contain: * Your Lambda handler files (``handler.py``, ``__init__.py``) * Project folders specified in ``include_project_folders`` (``commons/``) * Additional files from ``includes`` (``docs/``) * Python dependencies from ``requirements.txt`` (if present) Performance Tips =============================================================================== 1. **Use parallel execution for independent tests:** .. code-block:: shell pytest -n auto .. 2. **Run fast unit tests first during development:** .. code-block:: shell pytest tests/unit/ -n auto .. 3. **Run functional tests with limited parallelism:** .. code-block:: shell pytest tests/functional/ -n 2 # Avoid AWS rate limits .. 4. **Use markers to run specific test subsets:** .. code-block:: shell pytest -m "unit and not slow" -n auto ..