Skip to main content

Terraform fixtures for pytest

Project description

pytest-terra-fixt

With the use of this plugin, users can run Terraform commands in a unit/integration test fashion using the Pytest framework. The fixtures within this plugin use the awesome tftest Python package. Tftest is a simple Python wrapper that runs and parses the output of Terraform commands.

Fixtures

terraform_version:

  • Scope: Session
  • Return: Installs and/or sets the Terraform version to be used via tfenv

tf:

  • Scope: Session
  • Setup: Runs terraform init on the specified directory
  • Yield: tftest.TerraformTest object that can run subsequent Terraform commands with
  • Teardown: Runs terraform destroy -auto-approve on the specified directory

tf_factory:

  • Scope: Session
  • Setup: Runs terraform init on the specified directory
  • Yield: Factory fixture that returns a tftest.TerraformTest object that can run subsequent Terraform commands with
  • Teardown: Runs terraform destroy -auto-approve on the specified directory

All fixtures within this module are session-scoped given that Terraform consists of downloading providers and modules and of course spinning up time-consuming and possibly expensive resources. Using a session scope means that the fixtures are only invoked once per session unless they are skipped using the CLI arguments described in the following section.

CLI Arguments

--skip-tf-init: Skips running terraform init within tf and/or tf_factory fixtures. Useful for local testing of large modules where refreshing Terraform modules and providers is not needed on every pytest invocation.

--skip-tf-destroy: 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: If the user wants to continually preserve the Terraform tfstate, the --skip-tf-destroy flag needs to be always present, or else the `tf` or `tf_factory` fixture teardown will destroy the Terraform resources and remove the tfstate file.

Examples

fixtures/main.tf

output "foo" {
   value = "bar
}

test_plan.py via tf

 
import pytest
import logging
import os
 
@pytest.mark.parametrize('tf', [f'{os.path.dirname(__file__)}/fixtures'], indirect=True)
@pytest.mark.parametrize('terraform_version', ['latest', '0.15.0'], indirect=True)
def test_plan(tf, terraform_version):
   assert tf.plan().outputs['foo'] == 'bar'

test_plan.py via tf_factory

 
import pytest
import logging
import os
 
@pytest.mark.parametrize('terraform_version', ['latest', '0.15.0'], indirect=True)
def test_plan(tf, terraform_version):
   tf = tf_factory(f'{os.path.dirname(__file__)}/fixtures')
   assert tf.plan().outputs['foo'] == 'bar'

Installation

Install via Pip:

pip install pytest-terra-fixt-marshall7m

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-terra-fixt-0.4.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

pytest_terra_fixt-0.4.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest-terra-fixt-0.4.1.tar.gz.

File metadata

  • Download URL: pytest-terra-fixt-0.4.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for pytest-terra-fixt-0.4.1.tar.gz
Algorithm Hash digest
SHA256 7de387140cf4e55b05ab0860b32d64a73ea1f16f7bd71db842a7770f605e3d47
MD5 89a0234f811b173523625bdfc9a9c5a8
BLAKE2b-256 2835fb09f7fd1dfdc17bed03322f8335f4f9ca0f2d619ccab6f649961e924869

See more details on using hashes here.

File details

Details for the file pytest_terra_fixt-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_terra_fixt-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48c71db5132b55aa3bbd5ed479c2659da0a8d15e78a5a6835ef67e134440536e
MD5 9c7b7406b09db4e296c19e240fa1fe1e
BLAKE2b-256 3fc28ed02c076bb85b3a37996a212c60832a7dac9deaa451c29bf453a8e41b1d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page