带Pester测试的PowerShell-未找到测试文件,也未提供脚本块
尝试学习Pester(v5.0.4和PS v7.0.3)。我有这些文件 Get-Planet2.ps1:带Pester测试的PowerShell-未找到测试文件,也未提供脚本块,powershell,pester,Powershell,Pester,尝试学习Pester(v5.0.4和PS v7.0.3)。我有这些文件 Get-Planet2.ps1: function Get-Planet ([string]$Name = '*') { $planets = @( @{ Name = 'Mercury' } @{ Name = 'Venus' } @{ Name = 'Earth' } @{ Name = 'Mars' } @{ Name
function Get-Planet ([string]$Name = '*') {
$planets = @(
@{ Name = 'Mercury' }
@{ Name = 'Venus' }
@{ Name = 'Earth' }
@{ Name = 'Mars' }
@{ Name = 'Jupiter' }
@{ Name = 'Saturn' }
@{ Name = 'Uranus' }
@{ Name = 'Neptune' }
) | ForEach-Object { [PSCustomObject] $_ }
$planets | Where-Object { $_.Name -like $Name }
}
Get-Planet2.Tests.ps1:
BeforeAll {
# This will bring the function from the main file back to scope.
. $PSScriptRoot/Get-Planet2.ps1
param(
[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$name,
[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$title,
[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$InputFile
)
}
Describe 'Get-Planet' {
It 'Given no parameters, it lists all 8 planets' {
$allPlanets = Get-Planet
$allPlanets.Count | Should -Be 8
}
It 'Earth is the third planet in our Solar System' {
$allPlanets = Get-Planet
$allPlanets[2].Name | Should -Be 'Earth'
}
It 'Pluto is not part of our Solar System' {
$allPlanets = Get-Planet
$plutos = $allPlanets | Where-Object Name -EQ 'Pluto'
$plutos.Count | Should -Be 0
}
It 'Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune' {
$allPlanets = Get-Planet
$planetsInOrder = $allPlanets.Name -join ', '
$planetsInOrder | Should -Be 'Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune'
}
}
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$path = "$here/Get-Planet2.Tests.ps1"
$parameters = @{name = "John"; title = "Sales"; InputFile = "$here/test.json"}
write-host "Path: $path"
write-host $parameters
Invoke-Pester -Script @{Path=$path;Parameters=$parameters}
Call-Get-Planet2.ps1:
BeforeAll {
# This will bring the function from the main file back to scope.
. $PSScriptRoot/Get-Planet2.ps1
param(
[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$name,
[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$title,
[parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$InputFile
)
}
Describe 'Get-Planet' {
It 'Given no parameters, it lists all 8 planets' {
$allPlanets = Get-Planet
$allPlanets.Count | Should -Be 8
}
It 'Earth is the third planet in our Solar System' {
$allPlanets = Get-Planet
$allPlanets[2].Name | Should -Be 'Earth'
}
It 'Pluto is not part of our Solar System' {
$allPlanets = Get-Planet
$plutos = $allPlanets | Where-Object Name -EQ 'Pluto'
$plutos.Count | Should -Be 0
}
It 'Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune' {
$allPlanets = Get-Planet
$planetsInOrder = $allPlanets.Name -join ', '
$planetsInOrder | Should -Be 'Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune'
}
}
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$path = "$here/Get-Planet2.Tests.ps1"
$parameters = @{name = "John"; title = "Sales"; InputFile = "$here/test.json"}
write-host "Path: $path"
write-host $parameters
Invoke-Pester -Script @{Path=$path;Parameters=$parameters}
我的问题:
当我运行Call-Get-Planet2.ps1时,我以以下错误结束。我不明白它为什么这样做。需要指导。谢谢
System.Management.Automation.RuntimeException:未找到任何测试文件,也未提供任何脚本块。
在调用Pester时,/Users/myaccount/.local/share/powershell/Modules/Pester/5.0.4/Pester.psm1:第4520行
在,/Users/myaccount/repos/myrepo/Pester/Call-Get-Planet2.ps1:第8行
在,:第1行在深入研究这一问题之前,您回顾了哪些研究?例如,我读了《纠缠者》教程。我的文件主要来自教程。我通过向tests.ps1文件添加参数进行了更改。当我尝试添加invoke pester-script(带参数)时,它失败了,出现了上述错误。一直在做更多的研究,这可能是因为现在的v5.0.4在参数方面工作不太好。@UltraGC-从头开始:当您将cd放入包含测试文件的文件夹并简单地调用Pester时会发生什么。?@LievenKeersmaekers感谢您的建议。在运行invoke-pester时,我得到“术语'param'未被识别为cmdlet的名称,…”。我将“param”部分(在Get-Planet2.Tests.ps1中)移到了所有部分之前,但没有多大帮助。在5.0.4中,“参数”可能被忽略或不再需要?不确定,但我认为您需要将所有内容封装在函数中。您能否在测试代码周围包装一个
函数Get Planet Tests{
..}
?