为Pester V5测试提供测试用例
我正在尝试编写佩斯特测试(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果服务器和服务的列表经常更改,则最好是从单独的文件中读取它,尤其是如果您在版本控制下进行测试时。这样,您就可以在历史记录中轻松看到只有测试数据已更改,但是测试逻辑没有改变。
给定的测试数据的好文件格式是CSV:
您可以使用
import> import-csv
,但是您必须将每一行转换为hashtable
,因为期望-TestCases
参数有一系列的Hashtables。import-csv
输出pscustomObject
的数组。注意:虽然并非严格必要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:
You can read the CSV using
Import-Csv
, but you have to convert each row to ahashtable
, because Pester expects an array of hashtables for the-TestCases
parameter.Import-Csv
outputs an array ofPSCustomObject
though.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.