【问题标题】:Pester PowerShell testing for a sample script is not working as expected对示例脚本的 Pester PowerShell 测试未按预期工作
【发布时间】:2016-08-15 20:33:53
【问题描述】:

大家好,我编写了一个示例脚本来查找给定的字符串是否为回文,如下所示

function Palindrome1([string] $param)
{
  [string] $ReversString
  $StringLength = @()

  $StringLength = $param.Length

  while ( $StringLength -ge 0 )
 {
    $ReversString = $ReversString + $param[$StringLength]
    $StringLength--
 }    

 if($ReversString -eq $param)
 {
    return $true
 }
else
{
    return $false
}
}

这是我的.tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

Describe "Palindrome1" {
    It "does something useful" {
        Palindrome1 "radar" | Should Be $true
    }
}

下面是调用脚本

$modulePath = "D:\Pester-master\Pester.psm1"
$SourceDir = "E:\Pester"
Import-Module $modulePath -ErrorAction Inquire
$outputFile = Join-Path $SourceDir "TEST-pester.xml"

$result = Invoke-Pester -Path $SourceDir -CodeCoverage "$SourceDir\*.ps1" -PassThru -OutputFile $outputFile

$result

我没有得到预期的结果,有人能告诉我哪里做错了吗

【问题讨论】:

  • 你看到了什么结果?
  • 在我的xml中我得到如下<message>Expected: {true} But was: {}</message>

标签: powershell pester


【解决方案1】:

此声明:

[string] $ReversString

是一个导致空字符串的值表达式。每次运行时,Palindrome1 函数都会输出该空字符串。将其更改为:

[string] $ReversString = ''

【讨论】:

  • 您好,当我将输入参数设置为强制时,它要求我多次输入字符串function Palindrome1 { [CmdletBinding()] param ( [Parameter(Mandatory)] [string] $param )
  • 无法重现,如果参数不存在,它只会提示一次。如需参数帮助,请ask a new question
猜你喜欢
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 2018-04-24
相关资源
最近更新 更多