Skip to main content

Python Test Helper for AWS CDK

Python Version GitHub Workflow Status (with branch) GitHub PyPI PRs Welcome

This simple helper facilitates testing CDK constructs from Python unit tests, by wrapping the CDK executable and exposing convenience methods to set up fixtures, execute CDK commands, and parse their output.

It allows for different types of tests: lightweight tests that only use CDK synthesize to ensure code is syntactically correct and the right number and type of resources should be created, or full-fledged tests that run the full deploy cycle, and can then be used to test the actual created resources.

This tool is heavily inspired by this project: terraform-python-testing-helper.

Example Usage

The tests folder contains simple examples on how to write tests for both synth and deploy.

This is a test that uses synth output on an actual module:

import pytest
import cdktest
import json


@pytest.fixture
def output(fixtures_dir):
    cdk = cdktest.CDKTest("custom", fixtures_dir, binary="npx cdk")
    return cdk.synthesize()


def test_vpc_count(output):
    assert len(output.resources["AWS::EC2::VPC"]) == 1

def test_subnet_type(output):
    subnet_output = output.resources["AWS::EC2::Subnet"]
    tag_list = map(lambda x: x["Tags"], subnet_output)
    type_count = Counter(
        [
            item["Value"]
            for sublist in tag_list
            for item in sublist
            if item["Key"] == "aws-cdk:subnet-type"
        ]
    )
    assert (
        type_count["Private"] == 2
    ), f'Expected number of Private subnet is 2, got {type_count["Private"]}'
    assert (
        type_count["Public"] == 2
    ), f'Expected number of Public subnet is 2, got {type_count["Public"]}'

Caching

The CDKTest synthesize and deploy methods have the ability to cache its associate output to a local .cdktest-cache directory. This cache directory will work as a flag. For subsequent calls of the method, the cached folder will be recognized and avoid calling the actual underlying cdk command again and again. Using the cache flag can be significantly faster than running the cdk command again especially if the command is time-intensive.

The benefits of the caching feature include:

  • Faster setup time for testing cdk constructs that don't change between testing sessions

Please see the following example for how to use it:

import pytest
import cdktest


@pytest.fixture(scope="session")
def cdk(request, fixtures_dir):
    cdk = cdktest.CDKTest(
        appdir="no_change",
        basedir=fixtures_dir,
        binary="npx cdk",
        enable_cache=request.param,
    )
    yield cdk

    _LOGGER.debug("Removing cache dir")
    try:
        shutil.rmtree(cdk.cache_dir)
    except FileNotFoundError:
        _LOGGER.debug("%s does not exists", cdk.cache_dir)


@pytest.mark.parametrize("cdk", [True], indirect=True)
def test_use_cache(cdk):
    """
    Ensures cache is used and runs the execute_command() for first call of the
    method only
    """
    for method in cache_methods:
        with patch.object(
            cdk, "execute_command", wraps=cdk.execute_command
        ) as mock_execute_command:
            for _ in range(2):
                getattr(cdk, method)(use_cache=True)
            assert mock_execute_command.call_count == 1

Testing

Tests use the pytest framework and have no other dependency except on the Python cdk library.

Metadata

Release files for cdktest 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cdktest 0.0.1
File Size Uploaded
cdktest-0.0.1.tar.gz 12.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cdktest 0.0.1
File Interpreter ABI Platform
cdktest-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.2 kB

Release files / cdktest-0.0.1.tar.gz

Download URL cdktest-0.0.1.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
beecbd13eda370c160d366eac533d4426d9439a77dd74839a428051c396efc39
BLAKE2b-256 checksum
How to use checksums
463afb91c95dcdb1314b66ebfffdbc93d4abec4d6e54f9347181777f5dac319e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / cdktest-0.0.1-py3-none-any.whl

Download URL cdktest-0.0.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54c4c2dabe489a66be3be5496991dc3523605ff7f6ee2dc6d0c36432269d20f0
BLAKE2b-256 checksum
How to use checksums
747982741fdb4863470363bbcafefe8ea4f57407f0b69adf5c11ec22a0cf975e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page