Package: pytest-terra-fixt
pytest-terra-fixtWith 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 awesometftestPython package. In addition, given the lengthy time it takes to set up Terraform configurations, the fixtures use the PyTest built-inconfig.cachefixture to cache Terraform results that can be accessed across PyTest sessions.FixturesAll fixtures below can be parametrized via (@pytest.mark.parametrize())terra:Scope: SessionParameters [Required]:binary: Path to binary (must end withterraformor `terragrunt)tfdir: Absolute or relative path tobasedirbasedir: Base directory fortfdir(defaults to cwd)env: Environment variables to pass to the commandskip_teardown: Skips running fixture's teardown logicYield: Either thetftest.TerragruntTestortftest.TerraformTestclass depending on thebinaryparameterTeardown: Runsterraform destroy -auto-approveon thetfdirdirectory ifskip_teardownis set toFalseterra_setupScope: FunctionParameters [Optional]:
A: Dictionary of keyword arguments to passed to thetftest.TerraformTest.setup()method
B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned aboveReturns: Theterrafixture's associated setup outputterra_planScope: FunctionParameters [Optional]:
A: Dictionary of keyword arguments to passed to thetftest.TerraformTest.plan()method
B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned aboveReturns: Theterrafixture's associated plan outputterra_applyScope: FunctionParameters [Optional]:
A: Dictionary of keyword arguments to passed to thetftest.TerraformTest.apply()method
B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned aboveReturns: Theterrafixture's associated apply outputterra_outputScope: FunctionParameters [Optional]:
A: Dictionary of keyword arguments to passed to thetftest.TerraformTest.output()method
B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned aboveReturns: Theterrafixture's associated outputCLI Arguments--skip-teardown: Skips runningterraform destroy -auto-approveon 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.Examplesconftest.pyimport 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.pyimport 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"InstallationInstall via Pip:pip install pytest-terra-fixt