简体   繁体   English

Invoke-Pester -OutputFile 和 -OutputFormat 是旧参数集的成员

[英]Invoke-Pester -OutputFile and -OutputFormat are member of the legacy parameter set

In Pester 4 the commandlet offers the possibility to explicitly specify the OutputPath.在 Pester 4 中,命令行开关提供了显式指定 OutputPath 的可能性。

Invoke-Pester -Script $testFile -PassThru -Verbose -OutputFile $tr `
    -OutputFormat NUnitXml -CodeCoverage "$tmp/*-*.ps1" `
    -CodeCoverageOutputFile $cc -Show All

In version 5, this option is declared as a legacy parameter set and issues a corresponding warning.在版本 5 中,此选项被声明为旧参数集并发出相应的警告。

WARNING: You are using Legacy parameter set that adapts Pester 5 syntax to Pester 4 syntax. This parameter set is deprecated, and does not work 100%. The -Strict and -PesterOption parameters are ignored, and providing advanced configuration to -Path (-Script), and -CodeCoverage via a hash table does not work. Please refer to https://github.com/pester/Pester/releases/tag/5.0.1#legacy-parameter-set for more information.

Which implementation is planned for the following versions?以下版本计划采用哪种实施方式? If the parameter is no longer available, should the test results be extracted from the result object?如果参数不再可用,是否应该从结果 object 中提取测试结果?

There is pretty good documentation written for the new Pester 5 which you can find here: https://github.com/pester/Pester#simple-and-advanced-interface .有为新 Pester 5 编写的非常好的文档,您可以在这里找到: https : //github.com/pester/Pester#simple-and-advanced-interface

That link should take you to the specific section that you're looking for.该链接应将您带到您正在寻找的特定部分。

Essentially, they moved the configuration to an assembly class [PesterConfiguration] .本质上,他们将配置移至程序集类[PesterConfiguration] You can access the defaults by using [PesterConfigruation]::Default or more likely cast it to a new object that you will configure with your specific settings and output path.您可以使用[PesterConfigruation]::Default访问默认值,或者更有可能将其转换为一个新对象,您将使用特定设置和输出路径对其进行配置。 You could do that like this as an example:例如,您可以这样做:

$configuration = [PesterConfiguration]@{
    Run = @{
        Path = $testFile
    }
    Output = @{
        Verbosity = 'Detailed'
    }
    Filter = @{
        Tag = 'Acceptance'
        ExcludeTag = 'WindowsOnly'
    }
    Should = @{
        ErrorAction = 'Continue'
    }
    CodeCoverage = @{
        Enable = $true
        OutputPath = $yourPath
    }
}

You then pass that configuration object to Invoke-Pester.然后将该配置对象传递给 Invoke-Pester。 ie Invoke-Pester -Configuration $configurationInvoke-Pester -Configuration $configuration

You can still use some of the parameters in that 'legacy' style, Pester will just yell at you so that you aren't too surprised when it gets deprecated.您仍然可以以“传统”风格使用某些参数,Pester 只会对您大喊大叫,以便您在它被弃用时不会太惊讶。

As a side note- I do not see a nunit format for the test output so I don't know if they discontinued that.作为旁注 - 我没有看到测试输出的 nunit 格式,所以我不知道他们是否停止了这种格式。 The only one I see is 'JaCoCo'.我唯一看到的是“JaCoCo”。

I use Pester 5.1.1 to test Azure Resources after their deployment.我使用Pester 5.1.1在部署后测试 Azure 资源。 In Azure DevOps Services I execute a PowerShell task within a pipeline that triggers one script (Start-Pester.ps1) which in turn invokes tests from another script (PostDeployment.Tests.ps1) while passing the necessary parameter values.在 Azure DevOps Services 中,我在触发一个脚本 (Start-Pester.ps1) 的管道中执行PowerShell 任务,该脚本在传递必要参数值的同时从另一个脚本 (PostDeployment.Tests.ps1) 调用测试。

Start-Pester.ps1开始-Pester.ps1

param(
        [string]$SubscriptionId,
        [string]$TenantId,
        [string]$Username,
        [string]$Password,
        [string]$ResourceGroupName,
        [string]$FunctionAppName,
        [string]$EventHubNamespaceName,
        [string]$EventHubNamespaceAuthorizationRuleName,
        [string]$EventHubName,
        [string]$EventHubAuthorizationRuleName,
        [string]$EventHubAuthorizationRuleName1,
        [string]$ModulePath,
        [switch]$Publish,
        [string]$ResultsPath
    )
    
    [string]$SubscriptionId = (Get-Item env:SubscriptionId).value
    [string]$TenantId = (Get-Item env:TenantId).value
    [string]$Username = (Get-Item env:Username).value
    [string]$Password = (Get-Item env:Password).value
    [string]$ResourceGroupName = (Get-Item env:ResourceGroupName).value
    [string]$FunctionAppName = (Get-Item env:FunctionAppName).value
    [string]$EventHubNamespaceName = (Get-Item env:EventHubNamespaceName).value
    [string]$EventHubNamespaceAuthorizationRuleName = (Get-Item env:EventHubNamespaceAuthorizationRuleName).value
    [string]$EventHubName = (Get-Item env:EventHubName).value
    [string]$EventHubAuthorizationRuleName = (Get-Item env:EventHubAuthorizationRuleName).value
    [string]$EventHubAuthorizationRuleName1 = (Get-Item env:EventHubAuthorizationRuleName1).value
    
    $WarningPreference = "SilentlyContinue" 
    Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
    
    [array]$ModuleName = @("Az.Accounts", "Az.Resources", "Az.EventHub", "Az.Functions")
    foreach ($Module in $ModuleName) {
        Install-Module $Module -Scope CurrentUser -Force -SkipPublisherCheck -confirm:$false -AllowClobber
        Import-Module -Name $Module 
        Get-InstalledModule -Name $Module -AllVersions | Select-Object Name, Version
    }
    
    # Authentication
    $Credentials = New-Object System.Management.Automation.PSCredential ($Username, $(ConvertTo-SecureString $Password -AsPlainText -Force))
    Connect-AzAccount -Credential $Credentials -ServicePrincipal -Tenant $TenantId
    
    # Subscription
    Set-AzContext -Subscription $SubscriptionId
    
    $PesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -like '5.*' }
    if (!$PesterModule) { 
        try {
            Install-Module -Name Pester -Scope CurrentUser -Force -SkipPublisherCheck -MinimumVersion "5.0" -Repository PSGallery
            $PesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -like '5.*' }
        }
        catch {
            Write-Error "Failed to install the Pester module."
        }
    }
    
    Write-Host "Pester version: $($PesterModule.Version.Major).$($PesterModule.Version.Minor).$($PesterModule.Version.Build)"
    $PesterModule | Import-Module
    
    if ($Publish) {
        if (!(Test-Path -Path $ResultsPath)) {
            New-Item -Path $ResultsPath -ItemType Directory -Force | Out-Null
        }
    }
    
    $Tests = (Get-ChildItem -Path $($ModulePath) -Recurse | Where-Object { $_.Name -like "*Tests.ps1" }).FullName
    
    $Params = [ordered]@{
        Path = $Tests;
        Data = @{
            ResourceGroupName                      = $ResourceGroupName; 
            FunctionAppName                        = $FunctionAppName;
            EventHubNamespaceName                  = $EventHubNamespaceName;
            EventHubNamespaceAuthorizationRuleName = $EventHubNamespaceAuthorizationRuleName;
            EventHubName                           = $EventHubName;
            EventHubAuthorizationRuleName          = $EventHubAuthorizationRuleName;
            EventHubAuthorizationRuleName1         = $EventHubAuthorizationRuleName1;
        }
    }
    
    $Container = New-PesterContainer @Params
    
    $Configuration = [PesterConfiguration]@{
        Run          = @{
            Container = $Container
        }
        Output       = @{
            Verbosity = 'Detailed'
        }
        TestResult   = @{
            Enabled      = $true
            OutputFormat = "NUnitXml"
            OutputPath   = "$($ResultsPath)\Test-Pester.xml"
        }
        CodeCoverage = @{
            Enabled      = $true
            Path         = $Tests
            OutputFormat = "JaCoCo"
            OutputPath   = "$($ResultsPath)\Pester-Coverage.xml"
        }
    }
    
    if ($Publish) {
        Invoke-Pester -Configuration $Configuration
    }
    else {
        Invoke-Pester -Container $Container -Output Detailed
    }

PostDeployment.Tests.ps1部署后.测试.ps1

param(
    [string]$ResourceGroupName,
    [string]$FunctionAppName,
    [string]$EventHubNamespaceName,
    [string]$EventHubNamespaceAuthorizationRuleName,
    [string]$EventHubName,
    [string]$EventHubAuthorizationRuleName,
    [string]$EventHubAuthorizationRuleName1
)

Describe "Structure Tests" {

    BeforeAll {

        if ($ResourceGroupName.Length -gt 0) {
    
            $ResourceGroupData = Get-AzResourceGroup -Name $ResourceGroupName
        }

        if ($EventHubNamespaceName.Length -gt 0) {

            $EventHubNamespaceData = Get-AzEventHubNamespace -ResourceGroupName $ResourceGroupName -Name $EventHubNamespaceName
            $EventHubNamespaceAuthorizationRuleData = Get-AzEventHubAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -Name $EventHubNamespaceAuthorizationRuleName
        }

        if ($EventHubName.Length -gt 0) {

            $EventHubData = Get-AzEventHub -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -EventHubName $EventHubName
            $EventHubAuthorizationRuleData = Get-AzEventHubAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -EventHubName $EventHubName -Name $EventHubAuthorizationRuleName
            $EventHubAuthorizationRuleData1 = Get-AzEventHubAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -EventHubName $EventHubName -Name $EventHubAuthorizationRuleName1
        }

        if ($FunctionAppName.Length -gt 0) {

            $FunctionAppData = Get-AzFunctionApp -Name $FunctionAppName -ResourceGroupName $ResourceGroupName
        }
    }

    # Resource Group

    Context -Name "Resource Group" {

        It -Name "Passed Resource Group existence check" -Test {
            $ResourceGroupData | Should -Not -Be $null
        }        
    }

    # Event Hub Namespace

    Context -Name "Event Hub Namespace" {

        It -Name "Passed Event Hub Namespace existence check" -Test {
            $EventHubNamespaceData | Should -Not -Be $null
        }
        
        It -Name "Passed Event Hub Namespace tier check" -Test {
            $EventHubNamespaceData.Sku.Tier | Should -Be "Standard"
        }
        
        It -Name "Passed Event Hub Namespace TU check" -Test {
            $EventHubNamespaceData.Sku.Capacity | Should -Be 1
        }
        
        It -Name "Passed Event Hub Namespace auto-inflate check" -Test {
            $EventHubNamespaceData.IsAutoInflateEnabled | Should -Be $true
        }
        
        It -Name "Passed Event Hub Namespace maximum TU check" -Test {
            $EventHubNamespaceData.MaximumThroughputUnits | Should -Be 2
        }

        It -Name "Passed Event Hub Namespace shared access policies check" -Test {
            $EventHubNamespaceAuthorizationRuleData.Rights.Count | Should -Be 3
        }
    }

    # Event Hub

    Context -Name "Event Hub" {
    
        It -Name "Passed Event Hub existence check" -Test {
            $EventHubData | Should -Not -Be $null
        }
    
        It -Name "Passed Event Hub 'Listen' shared access policies check" -Test {
            $EventHubAuthorizationRuleData.Rights | Should -Be "Listen"
        }
    
        It -Name "Passed Event Hub 'Send' shared access policies check" -Test {
            $EventHubAuthorizationRuleData1.Rights | Should -Be "Send"
        }
    }

    # Function App

    Context -Name "Function App" {
    
        It -Name "Passed Function App existence check" -Test {
            $FunctionAppData | Should -Not -Be $null
        }        
        It -Name "Passed Function App AppSettings configuration existence check" -Test {
            $FunctionAppData.ApplicationSettings | Should -Not -Be $null
        }
        It -Name "Passed Function App APPINSIGHTS_INSTRUMENTATIONKEY existence check" -Test {
            $FunctionAppData.ApplicationSettings.APPINSIGHTS_INSTRUMENTATIONKEY | Should -Not -Be $null
        }  
        It -Name "Passed Function App FUNCTIONS_WORKER_RUNTIME value check" -Test {
            $FunctionAppData.ApplicationSettings.FUNCTIONS_WORKER_RUNTIME | Should -Be "dotnet"
        }
    }
}

As you can see I am overwriting [PesterConfigruation]::Default with my configuration.如您所见,我正在用我的配置覆盖 [PesterConfigruation]::Default。 And yes, TestResult block with NUnitXml works as well.是的,带有 NUnitXml 的 TestResult 块也可以工作。 Just add Publish Test Results task at the end of the pipeline.只需在管道末尾添加发布测试结果任务 It will pick up the test results and publish them.它将获取测试结果并发布它们。 Hope this will help someone in the future.希望这会在将来对某人有所帮助。

They moved many settings to a new Configuration object.他们将许多设置移至新的配置 object。 Described here: https://pester-docs.netlify.app/docs/commands/New-PesterConfiguration此处描述: https://pester-docs.netlify.app/docs/commands/New-PesterConfiguration

Old:老的:

Invoke-Pester -Script $testFile -PassThru -Verbose -OutputFile $tr `
    -OutputFormat NUnitXml -CodeCoverage "$tmp/*-*.ps1" `
    -CodeCoverageOutputFile $cc -Show All

New:新的:

$configuration = [PesterConfiguration]@{
      PassThru = $true
      Run = @{
         Path = $testFile
      }
      Output = @{
         Verbosity = 'Detailed'
      }
      TestResult = @{
         Enabled = $true
         OutputFormat = "NUnitXml"
         OutputPath   = $tr
         }
      CodeCoverage = @{
         Enabled = $true
         Path = "$tmp/*-*.ps1"
         OutputPath = $cc
         }
      }       

  Invoke-Pester -Configuration $configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM