英文:
Passing parameter to Pester -testcases
问题
我正在尝试找出参数化 Pester(版本 5.3.3)测试用例的最佳方法。情景是这样的。我想要检查一组登录是否存在于一组数据库实例中。我的代码非常重复,看起来像这样:
Describe 'Check that logins exist' {
It "Check that <value> exists on <dbInstance>" -Tag "DEV" -TestCases @(
@{ Value = "login1"; dbInstance = 'dev01'}
@{ Value = "login2"; dbInstance = 'dev01'}
@{ Value = "login3"; dbInstance = 'dev01'}
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
It "Check that <value> exists on <dbInstance>" -Tag "SIT" -TestCases @(
@{ Value = "login1"; dbInstance = 'sit01'}
@{ Value = "login2"; dbInstance = 'sit01'}
@{ Value = "login3"; dbInstance = 'sit01'}
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
}
我想要做的是参数化 dbInstance
值。这样,我的代码会更像这样:
Describe 'Check that logins exist' {
It "Check that <value> exists on <dbInstance>" -Tag "DEV" -TestCases @(
@{ Value = "login1"; dbInstance = '#dbInstance#'}
@{ Value = "login2"; dbInstance = '#dbInstance#'}
@{ Value = "login3"; dbInstance = '#dbInstance#'}
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
}
然后,当我运行我的测试时,我想要传递多个值给 #dbInstance#
,然后我想要代码对每个传递给 #dbInstance#
的值运行一次。这样,无论我需要针对多少个数据库实例运行它,我都能将代码重复最小化。希望这有意义。有人有任何好主意或我可以参考的示例吗?谢谢!
英文:
I am trying to figure out the best way to parameterise my Pester (V 5.3.3) testcases. The scenario is this. I would like to check that a list of logins exist on a group of database instances. My code is pretty repetitive, and looks like this:
Describe 'Check that logins exist' {
It "Check that <value> exists on <dbInstance>" -Tag "DEV" -TestCases @(
@{ Value = "login1"; dbInstance = 'dev01'}
@{ Value = "login2"; dbInstance = 'dev01'}
@{ Value = "login3"; dbInstance = 'dev01'}
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
It "Check that <value> exists on <dbInstance>" -Tag "SIT" -TestCases @(
@{ Value = "login1"; dbInstance = 'sit01'}
@{ Value = "login2"; dbInstance = 'sit01'}
@{ Value = "login3"; dbInstance = 'sit01'}
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
}
What I'd like to do is to parameterise that dbInstance value. That way, my code would look more like this:
Describe 'Check that logins exist' {
It "Check that <value> exists on <dbInstance>" -Tag "DEV" -TestCases @(
@{ Value = "login1"; dbInstance = #dbInstance#}
@{ Value = "login2"; dbInstance = #dbInstance#}
@{ Value = "login3"; dbInstance = #dbInstance#}
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
}
Then when I run my test, I'd like to pass multiple values to #dbInstance#, and then I'd like the code to run once per value passed to #dbInstance#. That way it doesn't matter how many database instances I need to run it against, and I'm keeping code repetition to a minimum. Hopefully that makes sense. Anyone got any good ideas, or examples I could follow?
Thanks a lot!
答案1
得分: 1
如果我理解您的意图正确,将您的测试包装在ForEach-Object
管道中,如下所示,可能会起作用:
Describe 'Check that logins exist' {
# 提供DB实例的名称作为输入。
'dev01', 'sit01' |
ForEach-Object {
$tag = $_.Substring(0, 3).ToUpper() # 推导出-Tag值
# 请注意在散列表中使用$_来引用当前的实例。
It "Check that <value> exists on <dbInstance>" -Tag $tag -TestCases @(
@{ Value = "login1"; dbInstance = $_ }
@{ Value = "login2"; dbInstance = $_ }
@{ Value = "login3"; dbInstance = $_ }
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
}
}
英文:
<!-- language-all: sh -->
If I understand your intent correctly, wrapping your test in a ForEach-Object
pipeline as follows may work:
Describe 'Check that logins exist' {
# Provide the names of the DB instances as input.
'dev01', 'sit01' |
ForEach-Object {
$tag = $_.Substring(0, 3).ToUpper() # derive the -Tag value
# Note the use of $_ in the hashtables to refer to the instance at hand.
It "Check that <value> exists on <dbInstance>" -Tag $tag -TestCases @(
@{ Value = "login1"; dbInstance = $_ }
@{ Value = "login2"; dbInstance = $_ }
@{ Value = "login3"; dbInstance = $_ }
) {
$login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
$login | Should -not -BeNullOrEmpty
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论