如何将参数传递给所有纠缠的测试脚本



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

那么,我们该怎么做呢?

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

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

param($param1, $param2)
Describe '' {
It '' { 
$param1 | Should -Be '...'
$param2 | Should -Be '...'
}
}

然后你这样运行它:

$container = New-PesterContainer -Path <tests_directory> -Data @{ param1='...'; param2='...' }
Invoke-Pester -Container $container

官方文件如下:https://pester.dev/docs/usage/data-driven-tests#providing-测试的外部数据

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

您可以通过向-Script参数传递一个散列数组来完成此操作。类似这样的东西:

$a = @()
$params = @{param1 = 'xx'; param2 = 'wuauserv'}
$a += @{Path =  '.test1.Tests.ps1'; Parameters = $params}
$a += @{Path =  '.test2.Tests.ps1'; Parameters = $params}
Invoke-Pester -Script $a

相关内容

最新更新