如何将参数传递给所有 pester 测试脚本

mar*_*ark 3 powershell pester

Invoke-Pester命令可以使用-Script参数调用具有显式参数的单个测试脚本。但是如果我想将相同的参数传递给所有的测试脚本呢?我不想在循环中调用 peter,因为我希望它生成单个测试结果文件。

那么,我们该怎么做呢?

Art*_*kov 5

从 Pester 5.1开始,您可以使用New-PesterContainer -Data @{}将所有必需的参数传递给Invoke-Pester. 您现在可以将单个测试文件或测试目录的路径传递给Invoke-Pester -Path.

例如,您有一个测试文件:

param($param1, $param2)

Describe '' {
  It '' { 
    $param1 | Should -Be '...'
    $param2 | Should -Be '...'
  }
}
Run Code Online (Sandbox Code Playgroud)

然后你像这样运行它:

$container = New-PesterContainer -Path <tests_directory> -Data @{ param1='...'; param2='...' }
Invoke-Pester -Container $container
Run Code Online (Sandbox Code Playgroud)

官方文档在这里:https : //pester.dev/docs/usage/data-driven-tests#providing-external-data-to-tests

您可以在此处找到新功能列表:https : //github.com/pester/Pester/releases/tag/5.1.0