Skip to content

Commit 47129e6

Browse files
committed
add autorest pester test clarification
1 parent e414f3b commit 47129e6

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

docs/getting-started.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,75 @@ A simple example is to change the param type from String to SecureString, to ach
8080
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
8181
8282
### Tests
83-
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.
84-
The docs for authoring the tests can be found at the following wiki
85-
https://github.com/Azure/azure-powershell/wiki/How-to-On-Board-New-RP-with-Azure-PowerShell-Generator#test
83+
#### Configuration
84+
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.
85+
86+
env.json
87+
```json
88+
{
89+
"Tenant": "11111111-1111-1111-1111-111111111111",
90+
"SubscriptionId": "11111111-1111-1111-1111-111111111111",
91+
"ResourceGroup": "MyResourceGroup",
92+
"Location": "Earth"
93+
}
94+
95+
```
96+
The loadEnv.ps1 file loads the env.json into $env hash table that is accessible in the current session of the calling script.
97+
98+
loadEnv.ps1
99+
```powershell
100+
$envFile = 'env.json'
101+
Write-Host "Loading env.json"
102+
if ($TestMode -eq 'live') {
103+
$envFile = 'localEnv.json'
104+
}
105+
106+
# Find the env.json file
107+
if (Test-Path -Path (Join-Path $PSScriptRoot $envFile)) {
108+
$envFilePath = Join-Path $PSScriptRoot $envFile
109+
} else {
110+
$envFilePath = Join-Path $PSScriptRoot '..\$envFile'
111+
}
112+
# Create $env for env.json file. Access $env from calling script to access env.json parameter values.
113+
$env = @{}
114+
if (Test-Path -Path $envFilePath) {
115+
# Convert env.json from json to powershell object.
116+
$env = Get-Content (Join-Path $PSScriptRoot $envFile) | ConvertFrom-Json
117+
$PSDefaultParameterValues=@{"*:SubscriptionId"=$env.SubscriptionId; "*:Tenant"=$env.Tenant}
118+
}
119+
```
120+
121+
Sample test calling loadEnv.ps1 to load env.json into variable $env. Access parameters like `Location` using `$env.Location`.
122+
123+
MyTest.Tests.ps1
124+
```powershell
125+
# Load the env.json file by running loadEnv.ps1
126+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
127+
if (-Not (Test-Path -Path $loadEnvPath)) {
128+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
129+
}
130+
. ($loadEnvPath)
131+
132+
$TestRecordingFile = Join-Path $PSScriptRoot 'MyTest.Recording.json'
133+
$currentPath = $PSScriptRoot
134+
while(-not $mockingPath) {
135+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
136+
$currentPath = Split-Path -Path $currentPath -Parent
137+
}
138+
. ($mockingPath | Select-Object -First 1).FullName
139+
140+
Describe "Tests" -Tags @('tag1', 'tag2') {
141+
# Loads a common file containing common features for tests, including an array of tests to skip, shared variables, and common functions.
142+
. $PSScriptRoot\Common.ps1
143+
144+
# Run the test, unless it is in list of $global:SkippedTests instantiated in Common.ps1.
145+
It "MyTest" -Skip:$('MyTest' -in $global:SkippedTests) {
146+
Write-Host "My Azure location: $($env.Location)"
147+
}
148+
}
149+
```
150+
151+
#### Official Autorest Documentation
152+
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.
153+
The docs for authoring the tests can be found at the following wiki:
154+
https://github.com/Azure/azure-powershell/wiki/How-to-On-Board-New-RP-with-Azure-PowerShell-Generator#test

0 commit comments

Comments
 (0)