Skip to content

Commit

Permalink
add autorest pester test clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOnlyWei committed Jul 20, 2021
1 parent e414f3b commit 47129e6
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,75 @@ A simple example is to change the param type from String to SecureString, to ach
4. Call the internal cmdlet with the updated PSBoundParameters https://github.com/azure/azurestack-powershell/blob/master/src/Azs.Backup.Admin/custom/Set-AzsBackupConfiguration.ps1
### Tests
Pester tests are required to make sure that cmdlets are validated against a live azurestack environment. Also to make sure that the future changes are not breaking the cmdlets. We follow the record and playback framework used by the azure powershell team.
The docs for authoring the tests can be found at the following wiki
https://github.com/Azure/azure-powershell/wiki/How-to-On-Board-New-RP-with-Azure-PowerShell-Generator#test
#### Configuration
The env.json file holds environment parameters. The parameter values in this file must be consistent with the values in the recording file. If they are not, you should replace the ones in the recording files with the parameters from the env.json or vice versa depending on which is more convenient.
env.json
```json
{
"Tenant": "11111111-1111-1111-1111-111111111111",
"SubscriptionId": "11111111-1111-1111-1111-111111111111",
"ResourceGroup": "MyResourceGroup",
"Location": "Earth"
}

```
The loadEnv.ps1 file loads the env.json into $env hash table that is accessible in the current session of the calling script.

loadEnv.ps1
```powershell
$envFile = 'env.json'
Write-Host "Loading env.json"
if ($TestMode -eq 'live') {
$envFile = 'localEnv.json'
}
# Find the env.json file
if (Test-Path -Path (Join-Path $PSScriptRoot $envFile)) {
$envFilePath = Join-Path $PSScriptRoot $envFile
} else {
$envFilePath = Join-Path $PSScriptRoot '..\$envFile'
}
# Create $env for env.json file. Access $env from calling script to access env.json parameter values.
$env = @{}
if (Test-Path -Path $envFilePath) {
# Convert env.json from json to powershell object.
$env = Get-Content (Join-Path $PSScriptRoot $envFile) | ConvertFrom-Json
$PSDefaultParameterValues=@{"*:SubscriptionId"=$env.SubscriptionId; "*:Tenant"=$env.Tenant}
}
```

Sample test calling loadEnv.ps1 to load env.json into variable $env. Access parameters like `Location` using `$env.Location`.

MyTest.Tests.ps1
```powershell
# Load the env.json file by running loadEnv.ps1
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
if (-Not (Test-Path -Path $loadEnvPath)) {
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
}
. ($loadEnvPath)
$TestRecordingFile = Join-Path $PSScriptRoot 'MyTest.Recording.json'
$currentPath = $PSScriptRoot
while(-not $mockingPath) {
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
$currentPath = Split-Path -Path $currentPath -Parent
}
. ($mockingPath | Select-Object -First 1).FullName
Describe "Tests" -Tags @('tag1', 'tag2') {
# Loads a common file containing common features for tests, including an array of tests to skip, shared variables, and common functions.
. $PSScriptRoot\Common.ps1
# Run the test, unless it is in list of $global:SkippedTests instantiated in Common.ps1.
It "MyTest" -Skip:$('MyTest' -in $global:SkippedTests) {
Write-Host "My Azure location: $($env.Location)"
}
}
```

#### Official Autorest Documentation
Pester tests are required to make sure that cmdlets are validated against a live azurestack environment. Also to make sure that the future changes are not breaking the cmdlets. We follow the record and playback framework used by the azure powershell team.
The docs for authoring the tests can be found at the following wiki:
https://github.com/Azure/azure-powershell/wiki/How-to-On-Board-New-RP-with-Azure-PowerShell-Generator#test

0 comments on commit 47129e6

Please sign in to comment.