Skip to main content

pytest-terra-fixt

With the use of this plugin, users can run Terragrunt and Terraform commands within parametrizable Pytest fixtures. Under the hood, the fixtures within this plugin use the awesome tftest Python package. In addition, given the lengthy time it takes to set up Terraform configurations, the fixtures use the PyTest built-in config.cache fixture to cache Terraform results that can be accessed across PyTest sessions.

Fixtures

All fixtures below can be parametrized via (@pytest.mark.parametrize())

terra:

  • Scope: Session
  • Parameters [Required]:
    • binary: Path to binary (must end with terraform or `terragrunt)
    • tfdir: Absolute or relative path to basedir
    • basedir: Base directory for tfdir (defaults to cwd)
    • env: Environment variables to pass to the command
    • skip_teardown: Skips running fixture's teardown logic
  • Yield: Either the tftest.TerragruntTest or tftest.TerraformTest class depending on the binary parameter
  • Teardown: Runs terraform destroy -auto-approve on the tfdir directory if skip_teardown is set to False

terra_setup

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.setup() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated setup output

terra_plan

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.plan() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated plan output

terra_apply

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.apply() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated apply output

terra_output

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.output() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated output

CLI Arguments

--skip-teardown: Skips running terraform destroy -auto-approve on teardown and preserves the Terraform backend tfstate for future testing. This flag is useful for checking out the Terraform resources within the cloud provider console or for running experimental tests without having to wait for the resources to spin up after every Pytest invocation.

NOTE: To continually preserve the Terraform tfstate, the `--skip-teardown` flag or the `"skip_teardown": True` attribute within the `terra` fixture parameters needs to be always present. If not, the `terra` fixture's teardown may destroy the Terraform resources and remove the tfstate files.

Examples

conftest.py

import pytest

def pytest_generate_tests(metafunc):
    if "terra" in metafunc.fixturenames:
        metafunc.parametrize(
            "terra",
            [
                pytest.param(
                    {
                        "binary": "terraform",
                        "skip_teardown": True,
                        "env": {
                           "TF_VAR_foo": "bar"
                        },
                        "tfdir": "fixtures",
                    },
                )
            ],
            indirect=True,
            scope="session",
        )

test_tf.py

import pytest

@pytest.mark.usefixtures("terra_setup", "terra_apply")
class TestModule:

   @pytest.mark.parametrize("terra_plan", [{"extra_files": ["plan.tfvars"]}])
   def test_plan(self, terra, terra_plan):
      assert terra_plan["bar"] == "zoo"

   def test_out(self, terra, terra_output):
      assert terra_output["doo"] == "foo"

Installation

Install via Pip:

pip install pytest-terra-fixt

Metadata

Release files for pytest-terra-fixt 0.6.2

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

Source distribution (sdist)

Source distribution for pytest-terra-fixt 0.6.2
File Size Uploaded Provenance
pytest-terra-fixt-0.6.2.tar.gz 4.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-terra-fixt 0.6.2
File Interpreter ABI Platform
pytest_terra_fixt-0.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / pytest-terra-fixt-0.6.2.tar.gz

Download URL pytest-terra-fixt-0.6.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b0fcf08652eb3c9479a8044a4d1b5d99a4dd00ddb337b7ece4a7120f2015ef6a
BLAKE2b-256 checksum
How to use checksums
06378a9defe300005d378915c9ea545d0509897edc587aa4098b1cd48eaf2447
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / pytest_terra_fixt-0.6.2-py3-none-any.whl

Download URL pytest_terra_fixt-0.6.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
508914cc98ee2cd38f6c0699e8d53070fb4bbcf46c1f8270ffa5e1cb5e281617
BLAKE2b-256 checksum
How to use checksums
5774f77ae01c0391746e1fdfb917edb71f735853289497e8d3c2b6730d0d5c01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Security for pytest-terra-fixt 0.6.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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