繁体   English   中英

带有 Pester 测试的 PowerShell - 未找到测试文件且未提供脚本块

[英]PowerShell with Pester Tests - No test files were found and no scriptblocks were provided

尝试学习 Pester(v5.0.4 和 PS v7.0.3)。 我有这些文件

获取-Planet2.ps1

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 }
}

获取-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'
    } 

}

Call-Get-Planet2.ps1:

$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:未找到测试文件且未提供脚本块。 在 Invoke-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 行

如果遇到此错误(“未找到测试文件且未提供LievenKeersmaekers' ”),则应遵循LievenKeersmaekers'建议(在问题评论中提出),即通过执行以下操作更直接地执行测试文件:

cd {TO\A\FOLDER\WITH\A\TEST_FILE}
Invoke-Pester -Path .\{TEST_FILE}.ps1

您可能会看到更具描述性的错误消息,可帮助您解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM