我正在Pester进行一些基础架构测试,并且有一些重复的场景,我不知道如何处理。
假设我想检查是否在IIS上启用了所有必需的Web角色。我有一组必需的Web角色,对于每个角色我想断言它已启用。
我目前的代码如下:
awk -F, '{sub(/.*word/,"word",$3);sub(/\..*/,"",$3);print $1,$2,$3}' OFS="," Input_file
它的工作原理是,如果未启用/安装任何角色,测试将失败。但是,如果这种Pester测试的输出看起来像这样,那就没什么用了:
$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")
}
}
}
......哪个更好,但感觉不太好......
此外,第二个问题是测试将在第一次失败时停止,我需要在修复每个功能后重新运行测试......
有什么想法吗?
答案 0 :(得分:2)
将It
置于循环中,如下所示:
Context "WebRoles" {
$requiredRole | ForEach-Object {
It "Has installed web role $_" {
(Get-WindowsOptionalFeature -FeatureName $_ -online).State | Should Be "Enabled"
}
}
}