我正在嘗試撰寫一個糾纏測驗(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 添加更多服務來修改我的糾纏檔案。對我這樣做感覺不太對勁,我想在這個早期階段讓方法正確。我的直覺告訴我,服務串列應該與糾纏檔案分開。然后運行測驗將涉及以某種方式匯入服務串列。有誰知道我是否以錯誤的方式進行此操作?感謝您的幫助安德魯
uj5u.com熱心網友回復:
如果服務器和服務串列經常更改,最好從單獨的檔案中讀取它,特別是如果您的測驗處于版本控制之下。這樣,您可以輕松地在歷史記錄中看到只有測驗資料發生了變化,而測驗邏輯沒有發生變化。
給定測驗資料的一個好的檔案格式是 CSV:
ComputerName, ServiceName
computer1, serviceA
computer1, serviceB
computer2, serviceB
您可以使用 讀取 CSV Import-Csv,但必須將每一行轉換為 a hashtable,因為 Pester 需要一個哈希表陣列作為-TestCases引數。輸出雖然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的建議將讀取測驗用例的代碼放入該BeforeDiscovery部分。這讓我們的意圖很明確。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472135.html
下一篇:我無法使用PHP插入的SQL值
