powershell - Pester如何测试powershell脚本不具备的功能?

标签 powershell pester

我有 Azure powershell 函数,run.ps1 看起来很漂亮

param($Request, $TriggerMetadata)
Write-Host $Request.Param1
Write-Host $Request.ParamB
Write-Host $Request.ParamC
return 200

它位于目录/func/myFunction/run.ps1 中 我的纠缠测试位于/func/Tests/my.Tests.ps1

测试看起来像

using namespace System.Diagnostics.CodeAnalysis

Describe "Test Run" {
    BeforeAll {
        $FunctionDir = $PSScriptRoot.Replace('Tests', 'myFunction')
        . $FunctionDir\run.ps1
    }

    It "Can get OK" {
        $Request = [PSCustomObject]@{
            ParamA = 'A'
            ParamB = 'B'
            ParamC = 'C'
        }
        $result = run -Request $Request
        $result | Should -Be 200
    }
}

当调用我的测试时,我得到“术语运行”无法识别...

如何使用 Pester 运行完整的脚本并测试其输出?

最佳答案

解决这个问题最简单的方法可能是使用 Set-Alias :

BeforeAll {
    $FunctionDir = $PSScriptRoot.Replace('Tests', 'myFunction')
    Set-Alias -Name Run -Value $FunctionDir\run.ps1
}

关于powershell - Pester如何测试powershell脚本不具备的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74625343/

相关文章:

.net - 通过反射加载程序集成功,Add-Type 不成功

powershell - 测试是否启用了 powershell 远程处理

powershell - 筛选Get-ADPrincipalGroupMembership

git - Azure Powershell - 如何进行 Github 同步?

unit-testing - Pester:并非所有模拟函数都被拦截

powershell - Pester 如何模拟 "test"模式中的 "test existence (not found) - create - test again to confirm creation"函数?

PowerShell Hyper-V VM 创建和启动

powershell - 如何为使用动态参数的高级函数 cmdlet 创建包装器

powershell - 如果Pester断言失败,该如何指定该怎么办?

class - Powershell 5 类的 Pester 模拟方法