Name | cdktest JSON |
Version |
0.0.1
JSON |
| download |
home_page | |
Summary | Simple python test helper for AWS CDK |
upload_time | 2023-06-23 07:44:24 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.10 |
license | Apache-2.0 |
keywords |
cdk
aws
python
test
helper
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<h1 align="center">Python Test Helper for AWS CDK</h1>
<p align="center">
<img alt="Python Version" src="https://img.shields.io/badge/python-3.10%20|%203.11-blue">
<img alt="GitHub Workflow Status (with branch)" src="https://img.shields.io/github/actions/workflow/status/leunguu/cdk-python-testing-helper/tests.yml?branch=main">
<img alt="GitHub" src="https://img.shields.io/github/license/leunguu/cdk-python-testing-helper">
<img alt="PyPI" src="https://img.shields.io/pypi/v/cdk-python-testing-helper">
<img alt="PRs Welcome" src=https://img.shields.io/badge/PRs-welcome-brightgreen.svg>
</p>
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](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper).
## Example Usage
The [`tests`](https://github.com/LEUNGUU/cdk-python-testing-helper/tree/main/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:
```python
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:
```python
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.
Raw data
{
"_id": null,
"home_page": "",
"name": "cdktest",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "CDK,AWS,Python,Test,Helper",
"author": "",
"author_email": "leunguu <liangy3928@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/46/3a/fb91c95dcdb1314b66ebfffdbc93d4abec4d6e54f9347181777f5dac319e/cdktest-0.0.1.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">Python Test Helper for AWS CDK</h1>\n\n<p align=\"center\">\n<img alt=\"Python Version\" src=\"https://img.shields.io/badge/python-3.10%20|%203.11-blue\">\n<img alt=\"GitHub Workflow Status (with branch)\" src=\"https://img.shields.io/github/actions/workflow/status/leunguu/cdk-python-testing-helper/tests.yml?branch=main\">\n<img alt=\"GitHub\" src=\"https://img.shields.io/github/license/leunguu/cdk-python-testing-helper\">\n<img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/cdk-python-testing-helper\">\n<img alt=\"PRs Welcome\" src=https://img.shields.io/badge/PRs-welcome-brightgreen.svg>\n</p>\n\nThis simple helper facilitates testing CDK constructs from Python unit tests, by wrapping the CDK executable and exposing\nconvenience methods to set up fixtures, execute CDK commands, and parse their output.\n\nIt allows for different types of tests: lightweight tests that only use CDK `synthesize` to ensure code is syntactically\ncorrect and the right number and type of resources should be created, or full-fledged tests that run the full `deploy` cycle,\nand can then be used to test the actual created resources.\n\nThis tool is heavily inspired by this project: [terraform-python-testing-helper](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper).\n\n## Example Usage\n\nThe [`tests`](https://github.com/LEUNGUU/cdk-python-testing-helper/tree/main/tests) folder contains simple examples on how to\nwrite tests for both `synth` and `deploy`.\n\nThis is a test that uses synth output on an actual module:\n\n```python\nimport pytest\nimport cdktest\nimport json\n\n\n@pytest.fixture\ndef output(fixtures_dir):\n cdk = cdktest.CDKTest(\"custom\", fixtures_dir, binary=\"npx cdk\")\n return cdk.synthesize()\n\n\ndef test_vpc_count(output):\n assert len(output.resources[\"AWS::EC2::VPC\"]) == 1\n\ndef test_subnet_type(output):\n subnet_output = output.resources[\"AWS::EC2::Subnet\"]\n tag_list = map(lambda x: x[\"Tags\"], subnet_output)\n type_count = Counter(\n [\n item[\"Value\"]\n for sublist in tag_list\n for item in sublist\n if item[\"Key\"] == \"aws-cdk:subnet-type\"\n ]\n )\n assert (\n type_count[\"Private\"] == 2\n ), f'Expected number of Private subnet is 2, got {type_count[\"Private\"]}'\n assert (\n type_count[\"Public\"] == 2\n ), f'Expected number of Public subnet is 2, got {type_count[\"Public\"]}'\n```\n\n## Caching\n\nThe CDKTest synthesize and deploy methods have the ability to cache its associate output to a local .cdktest-cache directory. This cache directory\nwill work as a flag. For subsequent calls of the method, the cached folder will be recognized and avoid calling the actual underlying `cdk` command\nagain and again. Using the cache flag can be significantly faster than running the `cdk` command again especially if the command is time-intensive.\n\nThe benefits of the caching feature include:\n\n - Faster setup time for testing cdk constructs that don't change between testing sessions\n\nPlease see the following example for how to use it:\n```python\nimport pytest\nimport cdktest\n\n\n@pytest.fixture(scope=\"session\")\ndef cdk(request, fixtures_dir):\n cdk = cdktest.CDKTest(\n appdir=\"no_change\",\n basedir=fixtures_dir,\n binary=\"npx cdk\",\n enable_cache=request.param,\n )\n yield cdk\n\n _LOGGER.debug(\"Removing cache dir\")\n try:\n shutil.rmtree(cdk.cache_dir)\n except FileNotFoundError:\n _LOGGER.debug(\"%s does not exists\", cdk.cache_dir)\n\n\n@pytest.mark.parametrize(\"cdk\", [True], indirect=True)\ndef test_use_cache(cdk):\n \"\"\"\n Ensures cache is used and runs the execute_command() for first call of the\n method only\n \"\"\"\n for method in cache_methods:\n with patch.object(\n cdk, \"execute_command\", wraps=cdk.execute_command\n ) as mock_execute_command:\n for _ in range(2):\n getattr(cdk, method)(use_cache=True)\n assert mock_execute_command.call_count == 1\n\n```\n## Testing\n\nTests use the `pytest` framework and have no other dependency except on the Python cdk library.\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Simple python test helper for AWS CDK",
"version": "0.0.1",
"project_urls": {
"homepage": "https://github.com/LEUNGUU/cdk-python-testing-helper",
"repository": "https://github.com/LEUNGUU/cdk-python-testing-helper"
},
"split_keywords": [
"cdk",
"aws",
"python",
"test",
"helper"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "747982741fdb4863470363bbcafefe8ea4f57407f0b69adf5c11ec22a0cf975e",
"md5": "1fcda121d21aacdde795e0cadc152015",
"sha256": "54c4c2dabe489a66be3be5496991dc3523605ff7f6ee2dc6d0c36432269d20f0"
},
"downloads": -1,
"filename": "cdktest-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1fcda121d21aacdde795e0cadc152015",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 10949,
"upload_time": "2023-06-23T07:44:22",
"upload_time_iso_8601": "2023-06-23T07:44:22.195074Z",
"url": "https://files.pythonhosted.org/packages/74/79/82741fdb4863470363bbcafefe8ea4f57407f0b69adf5c11ec22a0cf975e/cdktest-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "463afb91c95dcdb1314b66ebfffdbc93d4abec4d6e54f9347181777f5dac319e",
"md5": "3a9c0ea9ea84853238f101fc0aea683e",
"sha256": "beecbd13eda370c160d366eac533d4426d9439a77dd74839a428051c396efc39"
},
"downloads": -1,
"filename": "cdktest-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "3a9c0ea9ea84853238f101fc0aea683e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12291,
"upload_time": "2023-06-23T07:44:24",
"upload_time_iso_8601": "2023-06-23T07:44:24.350309Z",
"url": "https://files.pythonhosted.org/packages/46/3a/fb91c95dcdb1314b66ebfffdbc93d4abec4d6e54f9347181777f5dac319e/cdktest-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-23 07:44:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "LEUNGUU",
"github_project": "cdk-python-testing-helper",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "cdktest"
}