簡體   English   中英

Invoke-Pester -OutputFile 和 -OutputFormat 是舊參數集的成員

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

在 Pester 4 中,命令行開關提供了顯式指定 OutputPath 的可能性。

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

在版本 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.

以下版本計划采用哪種實施方式? 如果參數不再可用,是否應該從結果 object 中提取測試結果?

有為新 Pester 5 編寫的非常好的文檔,您可以在這里找到: https : //github.com/pester/Pester#simple-and-advanced-interface

該鏈接應將您帶到您正在尋找的特定部分。

本質上,他們將配置移至程序集類[PesterConfiguration] 您可以使用[PesterConfigruation]::Default訪問默認值,或者更有可能將其轉換為一個新對象,您將使用特定設置和輸出路徑對其進行配置。 例如,您可以這樣做:

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

然后將該配置對象傳遞給 Invoke-Pester。 Invoke-Pester -Configuration $configuration

您仍然可以以“傳統”風格使用某些參數,Pester 只會對您大喊大叫,以便您在它被棄用時不會太驚訝。

作為旁注 - 我沒有看到測試輸出的 nunit 格式,所以我不知道他們是否停止了這種格式。 我唯一看到的是“JaCoCo”。

我使用Pester 5.1.1在部署后測試 Azure 資源。 在 Azure DevOps Services 中,我在觸發一個腳本 (Start-Pester.ps1) 的管道中執行PowerShell 任務,該腳本在傳遞必要參數值的同時從另一個腳本 (PostDeployment.Tests.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
    }

部署后.測試.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"
        }
    }
}

如您所見,我正在用我的配置覆蓋 [PesterConfigruation]::Default。 是的,帶有 NUnitXml 的 TestResult 塊也可以工作。 只需在管道末尾添加發布測試結果任務 它將獲取測試結果並發布它們。 希望這會在將來對某人有所幫助。

他們將許多設置移至新的配置 object。 此處描述: https://pester-docs.netlify.app/docs/commands/New-PesterConfiguration

老的:

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

新的:

$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