如何为 PowerShell 脚本添加代码覆盖率

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

我的源代码位于 bitbucket 存储库中,我们正在使用 powershell 脚本运行 azure devops 管道。如何为我的 powershell 脚本提供代码覆盖率?

powershell azure-devops azure-pipelines bitbucket devops
1个回答
0
投票

BitBucket 本身在这种情况下并不一定重要。如果您有管道并且正在运行用 Pester 编写的 PowerShell 测试,则需要修改 PesterConfiguration 以支持测试输出和代码覆盖率,如 https://pester.dev/docs/usage/code-coverage

中所述
- task: PowerShell@2
  display: 'Run PowerShell Tests'
  inputs:
    pwsh: true
    targetType: 'inline'
    script: |

      $baseDirectory = "$(System.DefaultWorkingDirectory)"
      Push-Location $baseDirectory

      $container = New-PesterContainer `
         -Path (Get-ChildItem $baseDirectory -Include "*.Tests.ps1" -Recurse).FullName `
         -Data @{ Name = "$(Build.DefinitionName)" }

      $configuration = [PesterConfiguration]@{
        Run = @{
          Container = $container
          ExcludePath = (Get-ChildItem $baseDirectory -Include "*.psm1" -Recurse).FullName 
        }
        TestResult = @{
          Enabled = $true
          OutputFormat = "NUnitXml"
          OutputPath = (Join-Path "$(Agent.TempDirectory)" "TestResults.xml")
        }
        CodeCoverage = @{
          Enabled = $true
          OutputFormat = "JaCoCo"
          OutputPath = (Join-Path "$(Agent.TempDirectory)" "Pester-Coverage.xml")
        }
      }
  
      Invoke-Pester -Configuration $configuration

- task: PublishTestResults@2
  displayName: 'Publish Unit Test Results'
  inputs:
    testResultFormat: 'NUnit'
    testResultFiles: '$(Agent.TempDirectory)/TestResults.xml'
    failTaskOnFailedTests: true

- task: PublishCodeCoverageResults@1
  displayName: 'Publish Code Coverage'
  inputs:
    codeCoverageTool: 'JaCoCo'
    summaryFileLocation: '$(Agent.TempDirectory)/Pester-Coverage.xml'
    pathToSources: '$(System.DefaultWorkingDirectory)'

以上假设您的测试位于存储库的根目录中。

© www.soinside.com 2019 - 2024. All rights reserved.