How to Mock Pulumi Stack Outputs in Python

Create a dict-like mock of Pulumi stack outputs for local testing and scripts without running pulumi.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 12 views 0 copies

Python code

41 lines
Python 3.9+
from collections import defaultdict

class StackOutputMock:
    def __init__(self, outputs: dict):
        self.outputs = dict(outputs)
    
    def export(self):
        return self.outputs
    
    def get(self, key: str, default=None):
        return self.outputs.get(key, default)
    
    def keys(self):
        return self.outputs.keys()
    
    def values(self):
        return self.outputs.values()
    
    def items(self):
        return self.outputs.items()
    
    def __getitem__(self, key):
        return self.outputs[key]
    
    def __contains__(self, key):
        return key in self.outputs
    

if __name__ == "__main__":
    stack_output = StackOutputMock({
        "bucket_name": "my-app-assets",
        "api_url": "https://api.example.com/v1",
        "region": "us-east-1",
        "lambda_arn": "arn:aws:lambda:us-east-1:123456789012:function:processor"
    })
    
    print(stack_output.get("bucket_name"))
    print(stack_output["api_url"])
    print("region" in stack_output)
    print(stack_output.keys())
    print(list(stack_output.items()))

Output

stdout
my-app-assets
https://api.example.com/v1
True
dict_keys(['bucket_name', 'api_url', 'region', 'lambda_arn'])
[('bucket_name', 'my-app-assets'), ('api_url', 'https://api.example.com/v1'), ('region', 'us-east-1'), ('lambda_arn', 'arn:aws:lambda:us-east-1:123456789012:function:processor')]

How it works

This class wraps a plain dictionary so you can access Pulumi stack outputs with familiar dict-like syntax. The __getitem__ method enables bracket access, while get provides safe lookups with a default fallback. Magic methods like __contains__ make in checks work naturally. The class mimics the common subset of Pulumi's stack output API (get, keys, items) for local testing or standalone scripts. This lets you decouple your code from the Pulumi CLI during unit tests or when you only have static output values.

Common mistakes

  • Forgetting to use `dict(outputs)` to avoid aliasing the original dictionary
  • Only implementing `.get()` and forgetting `__getitem__` when code uses bracket access
  • Not implementing `__contains__` so `in` checks fail

Variations

  1. Use `SimpleNamespace` to access attributes like `stack_output.bucket_name`
  2. Use `types.MappingProxyType` to make the mock read-only

Real-world use cases

  • Unit testing infrastructure code that consumes Pulumi stack outputs without provisioning resources.
  • Standalone scripts that need to read stack outputs from a JSON fallback when running outside `pulumi up`.
  • CI pipelines that validate configuration files against expected stack output shapes before deployment.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.