You need to test a function that calls `subprocess.run(["terraform", "plan"])`. How?
IntermediateScenarioTesting, CI/CD, packaging & services
Answer
Situation read → Do not run Terraform in unit tests.
What I'd do →
- Patch
subprocess.runwhere it is used:monkeypatch.setattr(mymod.subprocess, "run", fake_run), returning asubprocess.CompletedProcess(args, 2, stdout=..., stderr=""). - Test parsing and decision logic for each return code (0 no changes, 2 changes with
-detailed-exitcode, 1 error). - Assert the exact arguments passed.
- A separate integration test can run real Terraform against a local backend.
Why → Unit tests check your logic; integration tests check the tool.
Interview tip → Mention -detailed-exitcode semantics.
More on Testing, CI/CD, packaging & services
You join a team whose Python automation repo has no tests. Where do you start?BeginnerYour pytest suite takes 12 minutes. How do you speed it up?IntermediateA test passes locally but fails in CI only when run with the rest of the suite. What do you suspect?IntermediateYou want a reusable CI pipeline for 15 Python tool repos. What goes in it?IntermediateYour Python Docker image is 1.1 GB. How do you shrink it?IntermediateYou want to distribute an internal CLI to 200 engineers. What are your options?Intermediate