Pester 测试用例在 BeforeDiscovery 中给出空值错误

问题描述 投票:0回答:1

每当我尝试在 Pester 测试用例的上下文下的 BeforeDiscovery 中使用参数值时,都会出现以下错误。请帮助我。

错误:: ##[错误] [-] D:\s\deploymenttest\deployment.Tests.ps1 中的发现失败,并显示: ##[错误] System.Management.Automation.ParameterBindingValidationException:无法验证参数“名称”的参数。参数为 null 或为空。提供一个不为 null 或空的参数,然后重试该命令。 ---> System.Management.Automation.ValidationMetadataException:参数为 null 或为空。提供一个不为 null 或空的参数,然后重试该命令。 ##[错误] 在 System.Management.Automation.ValidateNotNullOrEmptyAttribute.Validate(对象参数,EngineIntrinsics engineIntrinsics) ##[错误]在System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal参数,CompiledCommandParameter参数元数据,ParameterBindingFlags标志) ##[错误] 在 BeforeDiscovery,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 5749 行 ##[错误] 位于 D:\s\deploymenttest\deployment.Tests.ps1: 第 18 行 ##[错误] 在 New-Block,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 807 行 ##[错误] 在上下文中,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 8258 行 ##[错误] 位于 D:\s\deploymenttest\deployment.Tests.ps1: 第 16 行 ##[错误] 在 New-Block,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 807 行 ##[错误] 位于描述,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 9839 行 ##[错误] 位于 D:\s\deploymenttest\deployment.Tests.ps1: 第 14 行 ##[错误] 位于 C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 3034 行 ##[错误] 在调用文件,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 3043 行 ##[错误] 在 Invoke-BlockContainer,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 2942 行 ##[错误] 在 Discover-Test,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 1468 行 ##[错误] 在 Invoke-Test,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 2462 行 ##[错误] 在 Invoke-Pester,C:\Program Files\WindowsPowerShell\Modules\Pester .5.0\Pester.psm1:第 5046 行 ##[错误]位于,D:_templb6b5a-47a9-492f-9385-458fa30e1098.ps1:第59行 ##[错误] 位于 , : 第 1 行

这是我的 Powershell 纠缠代码。

param(
        [string]$environment
    )

BeforeDiscovery {
    $commonConfig = Import-PowerShellDataFile -Path .\parameters\common.psd1
}

BeforeAll {
    $rgConfig = Import-PowerShellDataFile -Path .\parameters\resourcegroup.psd1
}

Describe 'DevopsLab' {
    
    Context 'ResourceGroup' {

        BeforeDiscovery {
            $tmpTagList = (Get-AzResourceGroup -Name $rgconfig.$environment.rgname).Tags
            $tagList = @()

            foreach ($tagName in $tmpTagList.Keys) {
                $tagList += @{
                    name = $tagName
                    value = $tmpTagList.$tagName
                }
            }
        }


        It 'exists' {
            Get-AzResourceGroup -Name $rgconfig.$environment.rgname | Should -Not -BeNullOrEmpty
        }

        It 'all 3 tags are created' {
            (Get-AzResourceGroup -Name $rgconfig.$environment.rgname).Name
            (Get-AzResourceGroup -Name $rgconfig.$environment.rgname).Tags.Count | Should -BeGreaterOrEqual 3
        }

        it 'has an environment tag with value <environment>'{
            $envTagValue = (Get-AzResourceGroup -Name $rgconfig.$environment.rgname).Tags.Environment
            $envTagValue | Should -Be $environment
        }        
    }
}

Yaml 代码::

        - task: AzurePowerShell@5
          displayName: Deployment Validation Test Status
          inputs:
            azureSubscription: SC-Partner
            ScriptType: InlineScript
            Inline: |
                Set-Location $(System.DefaultWorkingDirectory)
                New-Item $(System.DefaultWorkingDirectory)\TestResults.xml -ItemType File

                $container = New-PesterContainer -Path ./deploymenttest/deployment.Tests.ps1 -Data @{ environment='dev'}
                $pesterConfig = [PesterConfiguration]@{
                  Run = @{
                      Exit = $true
                      Container = $container
                  }
                  Output = @{
                      Verbosity = 'Detailed'
                  }
                  TestResult = @{
                      Enabled      = $true
                      OutputFormat = "NUnitXml"
                      OutputPath   = "TestResults.xml"
                  }
                  Should = @{
                      ErrorAction = 'Stop'
                  }
                }
azure powershell azure-devops azure-pipelines-yaml pester
1个回答
0
投票

正如下面的 Pester 测试所证实的,由于它抛出异常,BeforeDiscovery 会在 BeforeAll 之前运行。因此,BeforeAll 中设置的变量在 BeforeDiscovery 运行时不可用。因此,您在第二个 BeforeDiscovery 内调用

Name
时传递给
Get-AzResourceGroup
参数的值可能是 $null。

BeforeAll {
    $paramDefinedInBeforeAll = 'foo'
}

Describe 'Test BeforeDiscovery/BeforeAll execution order' {
    BeforeDiscovery {
        if ($paramDefinedInBeforeAll -eq $null) {
            throw 'BeforeAll runs after BeforeDiscovery'
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.