如何将参数传递给所有的pester测试脚本
How to pass parameters to all the pester test scripts
Invoke-Pester
命令可以使用 -Script
参数调用带有显式参数的单个测试脚本。但是,如果我想将相同的参数传递给所有测试脚本怎么办?
我不想在循环中调用 pester,因为我希望它生成单个测试结果文件。
那么,我们该怎么做呢?
您可以通过将哈希数组传递给 -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
从 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-external-data-to-tests
您可以在此处找到新功能列表:https://github.com/pester/Pester/releases/tag/5.1.0
Invoke-Pester
命令可以使用 -Script
参数调用带有显式参数的单个测试脚本。但是,如果我想将相同的参数传递给所有测试脚本怎么办?
我不想在循环中调用 pester,因为我希望它生成单个测试结果文件。
那么,我们该怎么做呢?
您可以通过将哈希数组传递给 -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
从 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-external-data-to-tests
您可以在此处找到新功能列表:https://github.com/pester/Pester/releases/tag/5.1.0