Testing#

This project includes comprehensive test coverage with support for parallel execution.

Test Structure#

The project includes comprehensive test coverage:

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:

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:

pytest tests/functional/test_lambda_creation.py

Run specific test:

pytest tests/functional/test_lambda_creation.py::TestLambdaCreation::test_create_and_invoke_lambda_with_inline_code

Parallel Execution#

Install pytest-xdist:

pip install pytest-xdist
# or
pip install -e ".[dev]"

Run tests in parallel using all CPUs:

pytest -n auto

Run with specific number of workers:

pytest -n 4  # Use 4 parallel workers

Run functional tests with limited parallelism (recommended):

pytest tests/functional/ -n 2  # Avoid AWS rate limits

Test Markers#

Filter tests by markers:

# 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:

aws configure
  1. CDK CLI installed:

npm install -g aws-cdk
  1. 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):

pytest tests/functional/ -v -s

Parallel (faster but may hit rate limits):

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:

pytest -v   # Show test names
pytest -vv  # Show more details

Show print statements:

pytest -s  # Show stdout/stderr and logger output

Combine options:

pytest tests/functional/ -n auto -v -s

Debugging#

Show full traceback:

pytest --tb=long

Run only failed tests:

pytest --lf  # Last failed
pytest --ff  # Failed first

Stop on first failure:

pytest -x

Drop into debugger on failure:

pytest --pdb

Code Coverage#

Generate coverage report:

pytest --cov=core_aws_cdk --cov-report=html

Run with coverage in parallel:

pytest -n auto --cov=core_aws_cdk --cov-report=html

View HTML report:

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:

# 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:

python test.py

Expected output:

/path/to/project/.build/lambda_XXXXX.zip

Verifying Package Contents#

Inspect the generated ZIP file:

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:

    pytest -n auto
    
  2. Run fast unit tests first during development:

    pytest tests/unit/ -n auto
    
  3. Run functional tests with limited parallelism:

    pytest tests/functional/ -n 2  # Avoid AWS rate limits
    
  4. Use markers to run specific test subsets:

    pytest -m "unit and not slow" -n auto