为Pester V5测试提供测试用例

发布于 2025-01-27 04:39:07 字数 749 浏览 4 评论 0原文

我正在尝试编写佩斯特测试(V5),以查看是否在远程计算机上运行各种服务。这就是我所拥有的,它有效:

$Hashtable = @(
        @{ ComputerName = "computer1"; ServiceName = "serviceA" }
        @{ ComputerName = "computer1"; ServiceName = "serviceB" }
        @{ ComputerName = "computer2" ; ServiceName = "serviceB" }

    )


Describe "Checking services" {

        It "check <ServiceName> is running on <ComputerName>" -TestCases $Hashtable {
            ( get-service -computername $ComputerName -name $ServiceName ).status | Should -be "Running" 
        }

} 

我的问题是将测试数据提供给测试(即计算机名称和服务列表)。假设我想在此列表中添加更多服务。目前,我将通过将更多服务添加到$ Hashtable来修改我的Pester文件。对我这样做并不是很正确的,我想在这个早期阶段正确的方法。我的直觉告诉我,服务列表应与佩斯特文件分开。然后,运行测试将涉及以某种方式导入服务列表。有谁知道我是否以错误的方式进行操作? 感谢您的帮助 安德鲁

I'm trying to write a pester test (v5) to see if various services are running on remote computers. This is what I have, which works:

$Hashtable = @(
        @{ ComputerName = "computer1"; ServiceName = "serviceA" }
        @{ ComputerName = "computer1"; ServiceName = "serviceB" }
        @{ ComputerName = "computer2" ; ServiceName = "serviceB" }

    )


Describe "Checking services" {

        It "check <ServiceName> is running on <ComputerName>" -TestCases $Hashtable {
            ( get-service -computername $ComputerName -name $ServiceName ).status | Should -be "Running" 
        }

} 

My question is around providing the test data to the test (i.e. the list of computer names and services). Suppose I want to add more services to this list. At the moment, I would be modifying my pester file by adding more services to $Hashtable. It doesn't feel quite right to be doing this to me, and I'd like to get the approach correct at this early stage. My gut tells me that the list of services should be separated from the pester file. Then running the test would involve importing the list of services somehow. Does anyone know if I am going about this the wrong way?
Thanks for any help
Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-02-03 04:39:07

如果服务器和服务的列表经常更改,则最好是从单独的文件中读取它,尤其是如果您在版本控制下进行测试时。这样,您就可以在历史记录中轻松看到只有测试数据已更改,但是测试逻辑没有改变。

给定的测试数据的好文件格式是CSV:

ComputerName, ServiceName
computer1, serviceA
computer1, serviceB
computer2, serviceB

您可以使用 import> import-csv ,但是您必须将每一行转换为hashtable,因为期望-TestCases参数有一系列的Hashtables。 import-csv输出pscustomObject的数组。


BeforeDiscovery {
    $script:testCases = Import-Csv $PSScriptRoot\TestCases.csv | ForEach-Object {
        # Convert row (PSCustomObject) to hashtable.
        $hashTable = @{}
        $_.PSObject.Properties | ForEach-Object { $hashTable[ $_.Name ] = $_.Value }
        # Implicit output that will be captured in array $script:testCases
        $hashTable
    }
}

Describe "Checking services" {

    It "check <ServiceName> is running on <ComputerName>" -TestCases $script:testCases {
        ( get-service -computername $ComputerName -name $ServiceName ).status | Should -be "Running" 
    }
} 

注意:虽然并非严格必要docs.netlify.app/docs/usage/data-driven-tests#beforediscovery“ rel =“ nofollow noreferrer”> docs 。这表明我们的意图很明确。

If the list of servers and services will change often, it would be a good idea to read it from a separate file, especially if you have the tests under version control. This way you can easily see in the history that only the test data has changed, but the test logic didn't.

A good file format for the given test data would be CSV:

ComputerName, ServiceName
computer1, serviceA
computer1, serviceB
computer2, serviceB

You can read the CSV using Import-Csv, but you have to convert each row to a hashtable, because Pester expects an array of hashtables for the -TestCases parameter. Import-Csv outputs an array of PSCustomObject though.


BeforeDiscovery {
    $script:testCases = Import-Csv $PSScriptRoot\TestCases.csv | ForEach-Object {
        # Convert row (PSCustomObject) to hashtable.
        $hashTable = @{}
        $_.PSObject.Properties | ForEach-Object { $hashTable[ $_.Name ] = $_.Value }
        # Implicit output that will be captured in array $script:testCases
        $hashTable
    }
}

Describe "Checking services" {

    It "check <ServiceName> is running on <ComputerName>" -TestCases $script:testCases {
        ( get-service -computername $ComputerName -name $ServiceName ).status | Should -be "Running" 
    }
} 

Note: While not strictly necessary I have put the code that reads the test cases into the BeforeDiscovery section, as suggested by the docs. This makes our intentions clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文