pytest-terraform
pytest-terraform copied to clipboard
Support for combining multiple fixtures?
I was looking for a way to split up my terraform fixtures, to have a base fixture that could be used by more than one test, and then have per-test fixtures on top of those, while the per-test fixtures will maybe typically reference resources from the base fixture.
My naive attempt at doing this was by combining two @terraform
decorators, looking like this:
@terraform('my_base_fixture', scope='session')
@terraform('my_test_fixture')
def my_test(my_base_fixture, my_test_fixture):
pass
The result of that is that the first fixture is executed fine, but generating a terraform plan
for the second fixture fails, due to terraform not being able to look up referenced resources. Error: Reference to undeclared resource
I had some luck using Terraform data sources to be able to refer to resources in the base fixture from within the specialized test fixture. The remaining issue there was that I couldn't seemingly control the order of teardowns, such that the base fixture was always torn down first, resulting in failure during the tear down of the specialized fixture to look up the resources defined via data sources.
so there's two parts to this, a test using multiple fixtures, which is per the syntax above. the second part is the terraform modules referencing each other, which they do out of of band of pytest-terraform. from a disk location perspective its all relative addressing from the terraform module parent directory (--tf-dir) as we copy the modules into a temp dir for execution.