Write integration/recorded tests so they are deterministic, efficient in playback, and self-cleaning.
Apply these rules: 1) Deterministic errors → unit tests with mocks
2) Integration tests → narrow and service-focused
3) Assert the intended success and state changes
4) Always clean up in finally
finally.5) Keep recording/playback constants stable
Get-Date at runtime for values that are intended to remain constant across recording and playback.6) Use playback-aware waits
Start-TestSleep -Seconds <n> over Start-Sleep so playback can short-circuit.Example patterns:
# 1) Playback-aware wait
Start-TestSleep -Seconds 5
# 2) Strong success assertion (WhatIf)
Assert-NotNull $result
Assert-NotNull $result.Properties.ProvisioningState
Assert-AreEqual "Succeeded" $result.Properties.ProvisioningState
# 3) Cleanup in finally
try {
Enable-AzWhatever ...
}
finally {
# Always remove/delete/disable what you created/enabled
Remove-AzWhatever -Name $name -ErrorAction SilentlyContinue
}
# 4) Stable recording constant (avoid runtime Now)
$env.RecordDate = (Get-Date -Year 2025 -Month 10 -Day 25 -Hour 17 -Minute 31 -Second 02).ToString('dd-MM-yyyy-h-m-s')