{"id":21397592,"url":"https://github.com/multani/py-terratest","last_synced_at":"2025-03-16T15:17:49.452Z","repository":{"id":136260082,"uuid":"263968845","full_name":"multani/py-terratest","owner":"multani","description":"A Terratest-like test library for Python","archived":false,"fork":false,"pushed_at":"2020-05-19T21:53:33.000Z","size":3,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-23T02:24:40.086Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/multani.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-14T16:33:28.000Z","updated_at":"2022-03-16T15:41:09.000Z","dependencies_parsed_at":"2023-12-08T03:30:44.855Z","dependency_job_id":null,"html_url":"https://github.com/multani/py-terratest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fpy-terratest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fpy-terratest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fpy-terratest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multani%2Fpy-terratest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/multani","download_url":"https://codeload.github.com/multani/py-terratest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243886010,"owners_count":20363649,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-22T14:43:39.946Z","updated_at":"2025-03-16T15:17:49.411Z","avatar_url":"https://github.com/multani.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Terratest-like test library for Python\n\nThis is a [Terratest](https://terratest.gruntwork.io/)-like library containing\nhelpers for testing infrastructure.\n\n\u003e  It's a proof-of-concept, work-in-progress, not tested code.\n\u003e\n\u003e  So use at your own risk.\n\u003e\n\u003e Use [Terratest](https://terratest.gruntwork.io/) instead: it's more mature, has\n\u003e more features, is tested, has a bigger community, etc.\n\n\n## Why?\n\nWny not. I wanted to see how expressive my tests written using Terratest would\nhave been in Python.\n\n\n## How to use?\n\nIt's good to use it with [pytest](https://docs.pytest.org/)!\n\n### Terraform helpers\n\n`terratest.terraform.fixture` can be used to prepare [pytest\nfixtures](https://docs.pytest.org/en/latest/fixture.html):\n\n\n```python\nimport pytest\nfrom terratest import terraform\n\n@pytest.fixture\ndef terraform_web_server():\n    stage_name = \"setup-server\"\n    directory = \"../web-server\"\n    tf_vars = {\n        \"instance_type\": \"t3.medium\",\n    }\n\n    yield from terraform.fixture(stage_name, directory, tf_vars)\n```\n\nThis will run Terraform in the specified directory:\n\n* `terraform init` and `terraform apply` will be run as a test setup\n* `terraform destroy` will be run as a test teardown\n\nThe `apply` and `destroy` phases can be skipped using the following environment\nvariables:\n\n* `SKIP_${stage_name}_APPLY`: will skip the `apply` phase\n* `SKIP_${stage_name}_DESTROY`: will skip the `destroy` phase\n\nThis fixture can be chained together:\n\n```python\nimport pytest\nfrom terratest import terraform\n\n@pytest.fixture\ndef terraform_vpc():\n    yield from terraform.fixture(\"vpc\", \"../aws-vpc\")\n\n@pytest.fixture\ndef terraform_web_server(terraform_vpc):\n    yield from terraform.fixture(\"setup-server\", \"../web-server\")\n\n\ndef test_foobar(terraform_web_server):\n    # The VPC will be created, then the web server\n    # Tear down will be in the reversed order.\n```\n\n---\n\n`terratest.terraform.load_outputs` loads as a dictionary the outputs from a\nTerraform state:\n\n```python\nfrom terratest import terraform\n\ndef test_foobar(terraform_web_server):\n    # ...\n\n    output = terraform.load_outputs(\"../web-server\")\n    public_ip = output[\"public_ip\"]\n\n    #...\n```\n\n\n### AWS helpers\n\n`terratest.aws.get_instance_ids_for_asg` returns the instance IDs of the\nspecified AWS autoscaling group:\n\n```python\nfrom terratest import aws\n\ndef test_foo():\n    # ...\n\n    region = \"eu-central-1\"\n    asg = \"my-autoscaling-group\"\n    instance_ids = aws.get_instance_ids_for_asg(asg, region)\n\n    assert \"i-123456789123\" == instance_ids[0]\n```\n\n---\n\n`terratest.aws.get_public_ip_of_ec2_instance` returns the public IP address of\nthe specified AWS instance:\n\n```python\nfrom terratest import aws\n\ndef test_foo():\n    # ...\n\n    region = \"eu-central-1\"\n    instance_id = \"i-123456789123\"\n    public_ip = aws.get_public_ip_of_ec2_instance(instance_id, region)\n\n    assert \"8.8.8.8\" != public_ip\n```\n\n\n### Retrying tests\n\n`terratest.retry.retry` retries a block of code until it succeed or until a\ntimeout expires:\n\n```python\nfrom terratest.retry import retry\nimport random\n\ndef test_foo():\n    attempts = 10 # times\n    interval = 1  # seconds\n\n    for error_catcher in retry(\"rolling dices\", attempts, interval):\n        with error_catcher: # will catch any error in the block below\n            assert random.randint(0, 100) \u003c 50, \"not lucky yet\"\n```\n\nIf you prefer calling functions, you can use:\n\n```python\nfrom terratest.retry import retry\nimport random\n\ndef test_foo():\n    attempts = 10 # times\n    interval = 0.1 # seconds\n\n    def testing_dice(dice_size, chance):\n        assert random.randint(0, dice_size) \u003c chance, \"not lucky yet\"\n\n    size = 6\n    chance = 3\n    retry(\"rolling dices\", attempts, interval)(testing_dice, size, chance)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultani%2Fpy-terratest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmultani%2Fpy-terratest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultani%2Fpy-terratest/lists"}