Isolating Windows Registry Operations using the TestRegistry
TestRegistry is a Windows-only PowerShell PSDrive used to isolate registry based tests.
Pester creates a temporary, randomly named (a guid), registry key in the current user's hive under HKCU:\Software\Pester
which is accessible as TestRegistry:
.
Scoping​
Basic scoping rules are implemented for the TestRegistry in a similar way to TestDrive:
- A clean TestRegistry is created per container (test-file or scriptblock) on entry to the first top-level block (Describe or Context) in that container.
- All keys and values created by your setups and tests are available during the lifetime of that block, including inner blocks and tests.
- All keys are removed when exiting the block they were created in. Keys created in parent blocks is kept until that block is finished.
- When the whole container is finished, the temporary registry key is deleted and all the subkeys and values with it.
Cleanup is done at key-level
Be aware that cleanup in TestRegistry currently works on registry key-level. Values created inside an existing key from the parent block will persist until the key itself is deleted when the parent block is finished.
Example​
BeforeAll {
function Get-InstallPath($path, $key) {
Get-ItemProperty -Path $path -Name $key | Select-Object -ExpandProperty $key
}
}
Describe "Get-InstallPath" {
BeforeAll {
New-Item -Path TestRegistry:\ -Name TestLocation
New-ItemProperty -Path "TestRegistry:\TestLocation" -Name "InstallPath" -Value "C:\Program Files\MyApplication"
}
It 'reads the install path from the registry' {
Get-InstallPath -Path "TestRegistry:\TestLocation" -Key "InstallPath" | Should -Be "C:\Program Files\MyApplication"
}
}