Pester 测试不会因数组缺失值而失败
Pester test doesn't fail with the Array missing values
我们正在编写 Pester 测试以测试 Azure 资源组是否包含某些标记。以下是脚本,不幸的是,即使在我们正在检查的特定资源组不包含某些标签(来自定义的数组)之后,Pester 测试也没有报告任何失败。 Pester 测试刚刚通过,我不确定我们在这里做错了什么。
$resourceGroupName ='DemoRG03032021'
$listOfTags = @('BUSINESS-OWNER','COST-CENTER','LIFECYCLE1', 'APPLICATION','PROJECT-CODE','TECHNICAL-OWNER','BUDGET-CODE')
$checkTags = $false
Describe "Resource Group" {
Context "$resourceGroupName" {
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
foreach ($tagName in $listOfTags)
{
It "$($resourceGroup.ResourceGroupName) has a $tagName as tag" {
$resourceGroup.tags.keys -contains $tagName | Should -Be $true
}
}
}
}
在 v5 中,您现在也可以这样做,在我看来这更具可读性:
BeforeDiscovery {
$listOfTags = @('BUSINESS-OWNER', 'COST-CENTER', 'LIFECYCLE1', 'APPLICATION', 'PROJECT-CODE', 'TECHNICAL-OWNER', 'BUDGET-CODE')
}
BeforeAll {
$resourceGroupName = 'DemoRG03032021'
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
}
Describe "Resource Group" -ForEach $listOfTags {
It "$($resourceGroup.ResourceGroupName) has a $_ as tag" {
$resourceGroup.tags.keys -contains $_ | Should -Be $true
}
}
编辑:将您后续问题的答案放在这里,因为它更具可读性。
这就是我个人组织您在评论中发布的代码的方式。我认为添加另一个逻辑块实际上是有意义的。如果您将其他 It 语句放在 运行 -Foreach
块内,那么您也会 运行 每个新测试一次 $listOfTags
中的每个标记,这可能不是您想要的想要。
BeforeDiscovery {
$listOfTags = @('BUSINESS-OWNER', 'COST-CENTER', 'LIFECYCLE1', 'APPLICATION', 'PROJECT-CODE', 'TECHNICAL-OWNER', 'BUDGET-CODE')
}
Describe "Resource Group Tests" {
BeforeAll {
$resourceGroupName = 'TestResourceGroup203122021'
$resourceGroupLocation = 'eastus22222'
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
}
Context "Resource Group Tags" -ForEach $listOfTags {
It "$($resourceGroup.ResourceGroupName) has a $_ as tag" {
$resourceGroup.tags.keys -contains $_ | Should -Be $true
}
}
Context "Resource Group Attributes" {
It "Resource Group $($resourceGroup.ResourceGroupName) Exists" {
$resourceGroup | Should -Not -BeNullOrEmpty
}
It "$($resourceGroup.ResourceGroupName) Location is $resourceGroupLocation" {
$($resourceGroup.Location) | Should -Be $resourceGroupLocation
}
}
}
这是另一种思考方式。如果你写了以下内容:
Foreach ($tag in $listOfTags){
Write-Host 'do this thing for each tag'
Write-Host 'do this thing once'
}
想象一下,每个 Write-Host 都是您的 It 语句。您不希望第二个语句在与另一个相同的 context 中,因为您不希望它对 $listOfTags
中的每个值都 运行 一次.您在逻辑上用新的 Describe 或 Context 块将其分开。
我们正在编写 Pester 测试以测试 Azure 资源组是否包含某些标记。以下是脚本,不幸的是,即使在我们正在检查的特定资源组不包含某些标签(来自定义的数组)之后,Pester 测试也没有报告任何失败。 Pester 测试刚刚通过,我不确定我们在这里做错了什么。
$resourceGroupName ='DemoRG03032021'
$listOfTags = @('BUSINESS-OWNER','COST-CENTER','LIFECYCLE1', 'APPLICATION','PROJECT-CODE','TECHNICAL-OWNER','BUDGET-CODE')
$checkTags = $false
Describe "Resource Group" {
Context "$resourceGroupName" {
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
foreach ($tagName in $listOfTags)
{
It "$($resourceGroup.ResourceGroupName) has a $tagName as tag" {
$resourceGroup.tags.keys -contains $tagName | Should -Be $true
}
}
}
}
在 v5 中,您现在也可以这样做,在我看来这更具可读性:
BeforeDiscovery {
$listOfTags = @('BUSINESS-OWNER', 'COST-CENTER', 'LIFECYCLE1', 'APPLICATION', 'PROJECT-CODE', 'TECHNICAL-OWNER', 'BUDGET-CODE')
}
BeforeAll {
$resourceGroupName = 'DemoRG03032021'
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
}
Describe "Resource Group" -ForEach $listOfTags {
It "$($resourceGroup.ResourceGroupName) has a $_ as tag" {
$resourceGroup.tags.keys -contains $_ | Should -Be $true
}
}
编辑:将您后续问题的答案放在这里,因为它更具可读性。
这就是我个人组织您在评论中发布的代码的方式。我认为添加另一个逻辑块实际上是有意义的。如果您将其他 It 语句放在 运行 -Foreach
块内,那么您也会 运行 每个新测试一次 $listOfTags
中的每个标记,这可能不是您想要的想要。
BeforeDiscovery {
$listOfTags = @('BUSINESS-OWNER', 'COST-CENTER', 'LIFECYCLE1', 'APPLICATION', 'PROJECT-CODE', 'TECHNICAL-OWNER', 'BUDGET-CODE')
}
Describe "Resource Group Tests" {
BeforeAll {
$resourceGroupName = 'TestResourceGroup203122021'
$resourceGroupLocation = 'eastus22222'
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
}
Context "Resource Group Tags" -ForEach $listOfTags {
It "$($resourceGroup.ResourceGroupName) has a $_ as tag" {
$resourceGroup.tags.keys -contains $_ | Should -Be $true
}
}
Context "Resource Group Attributes" {
It "Resource Group $($resourceGroup.ResourceGroupName) Exists" {
$resourceGroup | Should -Not -BeNullOrEmpty
}
It "$($resourceGroup.ResourceGroupName) Location is $resourceGroupLocation" {
$($resourceGroup.Location) | Should -Be $resourceGroupLocation
}
}
}
这是另一种思考方式。如果你写了以下内容:
Foreach ($tag in $listOfTags){
Write-Host 'do this thing for each tag'
Write-Host 'do this thing once'
}
想象一下,每个 Write-Host 都是您的 It 语句。您不希望第二个语句在与另一个相同的 context 中,因为您不希望它对 $listOfTags
中的每个值都 运行 一次.您在逻辑上用新的 Describe 或 Context 块将其分开。