【问题标题】:Is there a way to test / assert output from Write-Host using Pester?有没有办法使用 Pester 测试/断言 Write-Host 的输出?
【发布时间】:2020-03-03 15:44:34
【问题描述】:

我正在为一个相当复杂的脚本编写测试,脚本中有一个特定的函数可以向用户输出不同系列的日志消息。我想断言是否正在显示特定的日志消息。

主要问题是我不知道是什么参数隐式处理我传递给Write-Host cmdlet 的文本。

这里有一些代码捕捉到我正在尝试做的事情的前提......

要测试的函数

function TestFunction($TestInput) {
    if ($TestInput -contains 1) {
        Write-Host "TestInput contains a 1"
    }

    if ($TestInput -contains 3 ) {
        Write-Host "TestInput contains a 3"
    }

    if ($TestInput -contains 4 ) {
        Write-Host "TestInput contains a 4"
    }
}

Pester 测试

Describe "TestFunction" {
    It "should call Write-Host with appropriate message if 1 or 3 is passed" {
        Mock Write-Host {}
        TestFunction -TestInput @(1, 2, 3)
        Assert-MockCalled Write-Host -Exactly 2 -Scope It
        Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { "TestInput contains a 1" }
        Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { "TestInput contains a 3" }
    }
}

输出

  Describing TestFunction
    [-] should call Write-Host with appropriate message if 1 or 3 is passed 19ms
      at <ScriptBlock>, : line 235
      235:         Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { "TestInput contains a 1" }
      Expected Write-Host to be called 1 times exactly but was called 2 times
Tests completed in 106ms
Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0 

【问题讨论】:

    标签: powershell pester


    【解决方案1】:

    查看Microsoft documentation for the Write-Host cmdlet后发现有一个-Object参数。这接受要写入控制台的通用对象数组。 -Object 参数是需要在 Pester 测试的 -ParameterFilter 中指定的参数,以断言正在显示适当的文本。

    我的更新代码如下...

    要测试的函数

    function TestFunction($TestInput) {
        if ($TestInput -contains 1) {
            Write-Host "TestInput contains a 1"
        }
    
        if ($TestInput -contains 3 ) {
            Write-Host "TestInput contains a 3"
        }
    
        if ($TestInput -contains 4 ) {
            Write-Host "TestInput contains a 4"
        }
    }
    

    Pester 测试

    Describe "TestFunction" {
        It "should call Write-Host with appropriate message if 1 or 3 is passed" {
            Mock Write-Host {}
            TestFunction -TestInput @(1, 2, 3)
            Assert-MockCalled Write-Host -Exactly 2 -Scope It
            Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "TestInput contains a 1" }
            Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "TestInput contains a 3" }
        }
    }
    

    输出

      Describing TestFunction
        [+] should call Write-Host with appropriate message if 1 or 3 is passed 20ms
    Tests completed in 99ms
    Tests Passed: 1, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0 
    

    【讨论】:

    • -Times 2 -Exactly,还有一些奇怪的原因,-Scope It 只有在It 区分大小写时才对我正常工作,否则it 也在计算其他 it 块的结果
    猜你喜欢
    • 2021-08-25
    • 2016-03-30
    • 2021-05-12
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多