传递参数给 Pester -testcases

huangapple go评论105阅读模式
英文:

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 &#39;Check that logins exist&#39; {

    It &quot;Check that &lt;value&gt; exists on &lt;dbInstance&gt;&quot; -Tag &quot;DEV&quot; -TestCases @(
        @{ Value = &quot;login1&quot;; dbInstance = &#39;dev01&#39;}
        @{ Value = &quot;login2&quot;; dbInstance = &#39;dev01&#39;}
        @{ Value = &quot;login3&quot;; dbInstance = &#39;dev01&#39;}
    ) {
        $login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
        $login | Should -not -BeNullOrEmpty
    }


    It &quot;Check that &lt;value&gt; exists on &lt;dbInstance&gt;&quot; -Tag &quot;SIT&quot; -TestCases @(
        @{ Value = &quot;login1&quot;; dbInstance = &#39;sit01&#39;}
        @{ Value = &quot;login2&quot;; dbInstance = &#39;sit01&#39;}
        @{ Value = &quot;login3&quot;; dbInstance = &#39;sit01&#39;}
    ) {
        $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 &#39;Check that logins exist&#39; {

    It &quot;Check that &lt;value&gt; exists on &lt;dbInstance&gt;&quot; -Tag &quot;DEV&quot; -TestCases @(
        @{ Value = &quot;login1&quot;; dbInstance = #dbInstance#}
        @{ Value = &quot;login2&quot;; dbInstance = #dbInstance#}
        @{ Value = &quot;login3&quot;; 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 &#39;Check that logins exist&#39; {

  # Provide the names of the DB instances as input.
  &#39;dev01&#39;, &#39;sit01&#39; |
    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 &quot;Check that &lt;value&gt; exists on &lt;dbInstance&gt;&quot; -Tag $tag -TestCases @(
        @{ Value = &quot;login1&quot;; dbInstance = $_ }
        @{ Value = &quot;login2&quot;; dbInstance = $_ }
        @{ Value = &quot;login3&quot;; dbInstance = $_ }  
      ) {
        $login = Get-DbaLogin -sqlinstance $dbInstance -SqlCredential $cred -Login $Value
        $login | Should -not -BeNullOrEmpty
      }

    }
 
}

huangapple
  • 本文由 发表于 2023年7月27日 21:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定