在Pester中收集每个项目的声明
问题描述:
我正在Pester中进行一些基础结构测试,并且存在重复的情况,我不知道如何处理。在Pester中收集每个项目的声明
比方说,我想检查是否在IIS上启用了所有必需的Web角色。我有一个必需的Web角色集合,并且对于每个角色我想声明它已启用。
我当前的代码如下所示:
$requiredRoles = @(
"Web-Default-Doc",
"Web-Dir-Browsing",
"Web-Http-Errors",
"Web-Static-Content",
"Web-Http-Redirect"
)
Context "WebRoles" {
It "Has installed proper web roles" {
$requiredRoles | % {
$feature = Get-WindowsOptionalFeature -FeatureName $_ -online
$feature.State | Should Be "Enabled"
}
}
}
它工作在这个意义上,如果任何一个角色都没有启用/安装会导致测试失败。但是,如果是这样的纠缠测试的输出看起来像这样几乎没有用处:
Context WebRoles
[-] Has installed proper web roles 2.69s
Expected: {Enabled}
But was: {Disabled}
283: $feature.State | Should Be "Enabled"
这个结果没有给出任何关于该产品采用的是一个残疾人任何线索。
在这些情况下是否有建议的做法?我曾想过一些字符串操作...
Context "WebRoles" {
It "Has installed proper web roles" {
$requiredRoles | % {
$feature = Get-WindowsOptionalFeature -FeatureName $_ -online
$toCompare = "{0}_{1}" -f $feature.FeatureName,$feature.State
$toCompare | Should Be ("{0}_{1}" -f $_,"Enabled")
}
}
}
这将输出:
Context WebRoles
[-] Has installed proper web roles 2.39s
Expected string length 27 but was 28. Strings differ at index 20.
Expected: {IIS-DefaultDocument_Enabled}
But was: {IIS-DefaultDocument_Disabled}
-------------------------------^
284: $toCompare | Should Be ("{0}_{1}" -f $_,"Enabled")
...这是更好的,但它并没有感觉非常好......
此外,还有第二个问题,测试将停止在第一次失败,我需要重新运行测试后,我修复每个功能...
任何想法?
答
把你It
内循环,像这样:
Context "WebRoles" {
$requiredRole | ForEach-Object {
It "Has installed web role $_" {
(Get-WindowsOptionalFeature -FeatureName $_ -online).State | Should Be "Enabled"
}
}
}
哇,我不知道我能做到这一点!这解决了我的两个问题 - 结果表现力和第一次失败时停下来。非常好!谢谢! – jumbo