powershell - 使用 `pester` 测试 `pester` 版本

标签 powershell testing pester

我有一套pester测试使用 v4.0+ should 运算符 -FileContentMatch 的 PowerShell 模块。当这些测试在具有早期 v3.x 版本的 pester 的计算机上运行时,会出现一波错误消息,但没有一条错误消息能够准确指出问题。

我想编写一个简单的 pester 测试来检查最低版本并为用户/测试人员打印解释/修复。

额外的复杂性是,pester 可以直接作为脚本运行,而无需作为模块安装在计算机上。

我考虑过使用 $(Get-Module -ListAvailable -name "Pester").version 来提取 pester 版本,但它只看到PowerShell-“已安装”模块,而不是当前执行的版本,如前所述,当前执行的版本可能不同。

pester 传递的一些信号没问题,但我没有看到 pester 为测试脚本提供任何元信息(即版本环境变量) )。

对解决方案有什么想法吗?

最佳答案

用于检查当前加载的 pester 模块版本的代码如下所示:

$pesterModules = @( Get-Module -Name "Pester" -ErrorAction "SilentlyContinue" );
if( ($null -eq $pesterModules) -or ($pesterModules.Length -eq 0) )
{
    throw "no pester module loaded!";
}
if( $pesterModules.Length -gt 1 )
{
    throw "multiple pester modules loaded!";
}
if( $pesterModules[0].Version -ne ([version] "4.8.0") )
{
    throw "unsupported pester version '$($pesterModules[0].Version)'";
}

但是选择在哪里运行它有点棘手 - 如果您可以在 pester 测试中运行它,那么将为您提供当前运行测试的 pester 版本 - 例如

Describe "check test suite version" {
    Context "pester version" {
        It "should be correct version" {
            ... version check code here ...
        }
    }
}

但是,无论是否加载了正确的版本,它仍然会运行您的主要测试,因此如果加载了错误的版本,您可能会收到大量背景噪音。

您可以运行上面的测试作为主要测试的飞行前检查 - 使用 -PassThru 调用 Invoke-Pester切换以检查结果,并且仅在预检测试通过时调用您的主要测试。

或者只是从已知位置自行加载 pester 模块,然后调用上面的代码块来验证版本。

关于powershell - 使用 `pester` 测试 `pester` 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733313/

相关文章:

powershell - PowerShell属性问题

java - Spring MVC 测试框架因 HTTP 响应 406 而失败

powershell - 纠缠:无法访问父作用域变量

javascript - 测量 JavaScript 函数的内存使用、执行时间和性能

java - 在测试期间更改许多 Spring bean 的范围

powershell - Pester 5.0.2 BeforeAll block 代码未显示在 Describe block 中

pester - 如何使用 Pester 模拟模块不可用的 cmdlet?

来自 ValidateSet 的 Powershell 多个函数参数

windows - ForEach ID 的 Group By ID

windows - 如何以与 "cmd/c <command>".execute() 相同的方式从 groovy 运行 PowerShell 命令?