Pracuję nad niektórymi sprawami testowymi Pester i czekam na wyniki CodeCoverage. W większości zestawów przypadków testowych, w których kod zawiera próbę/catch, otrzymujemy 0% zasięgu na haczyku. Oto przykład:Czy Pester może próbować wyjątku?
function Test-Is64Bit()
{
$Result = $false
try
{
if ((Get-WmiObject -Class "Win32_OperatingSystem").OSArchitecture -eq '64-bit')
{
$Result = $true
}
}
catch
{
Write-Error $_.Exception | Format-List -Force
}
return $Result
}
To dość łatwe, aby sfałszować wartość zwracaną Get-WmiObject, aby przetestować warunek $ true.
Wypróbowałem już kilka pomysłów, aby wyśmiać wyjątek od Get-WmiObject, ale w każdym przypadku wyjątek przechodzi na konsolę, zamiast zostać złapany przez Pester i zdać test. Poniżej znajduje się najlepsze, jakie wymyśliłem, ale nadal nie działa.
Context "Unit tests for Get-WmiObject exception" {
# mock the Get-WmiObject cmdlet for unit testing
Mock Get-WmiObject {
Throw
} -ParameterFilter { $Class -And $Class -eq 'Win32_OperatingSystem' }
It "Function test passes" {
{ Test-Is64Bit } | Should Be $false
Assert-MockCalled Get-WmiObject -Scope It -Exactly -Times 1
}
}
Ten test prowadzi:
Context Unit tests for Get-WmiObject error
[-] Function test passes 138ms
Expected: {False}
But was: { Test-Is64Bit }
at line: 62 in .\Tests\Test-Is64Bit.Tests.ps1
62: { Test-Is64Bit } | Should Be $false
Tests completed in 590ms
Tests Passed: 4 Failed: 1 Skipped: 0 Pending: 0 Inconclusive: 0
Code coverage report:
Covered 80.00 % of 10 analyzed Commands in 1 File.
Missed commands:
File Function Line Command
---- -------- ---- -------
Test-Is64Bit.ps1 Test-Is64Bit 38 Write-Error $_.Exception
Test-Is64Bit.ps1 Test-Is64Bit 38 Format-List -Force
Czy jakikolwiek sposób mock wyjątek jest rzucony przez Get-WmiObject więc możemy mieć Pester wpaść połowu i wreszcie osiągnąć 100% pokrycia kodu?
Mój obecny kod testowy szuka wyjątkami
Context "Unit tests for Get-WmiObject exception" {
# mock the Get-WmiObject cmdlet for unit testing
Mock Get-WmiObject {
Throw 'Some Error'
} -ParameterFilter { $Class -And $Class -eq 'Win32_OperatingSystem' }
It 'Get-WmiObject should throw' {
{ Get-WmiObject -Class 'Win32_OperatingSystem' } | Should Throw 'Some Error'
}
It "Throws exception" {
{ Test-Is64Bit } | Should Throw 'Some Error'
Assert-MockCalled Get-WmiObject -Scope It -Exactly -Times 1
}
}
Powyższe wyniki Kod w tym:
Context Unit tests for Get-WmiObject exception
[+] Get-WmiObject should throw 89ms
Test-Is64Bit : System.Management.Automation.RuntimeException: Some Error At
C:\ONESolutionFinance\Main\ReleaseManagement\PowerShell-Module\SPSNoDeps\Tests\Test-Is64Bit.Tests.ps1:66
char:7
+ { Test-Is64Bit } | Should Throw 'Some Error'
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-Is64Bit
[-] Throws exception 51ms
Expected: the expression to throw an exception with message {Some Error}, an exception was not raised, message was {}
from line:2 char:5
+ Throw 'Some Error'
+ ~~~~~~~~~~~~~~~~~~
at line: 66 in C:\ONESolutionFinance\Main\ReleaseManagement\PowerShell-Module\SPSNoDeps\Tests\Test-Is64Bit.Tests.ps1
66: { Test-Is64Bit } | Should Throw 'Some Error'
Testowanie za $ fałszywych zwraca ten:
Context Unit tests for Get-WmiObject exception
[+] Get-WmiObject should throw 162ms
Test-Is64Bit : System.Management.Automation.RuntimeException: Some Error
At C:\ONESolutionFinance\Main\ReleaseManagement\PowerShell-Module\SPSNoDeps\Tests\Test-Is64Bit.Tests.ps1:66 char:5
+ Test-Is64Bit | Should Be $false
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-Is64Bit
[+] Throws exception 92ms
Zaktualizowałem mój pierwotny wpis, aby pytanie stało się bardziej przejrzyste. Prześmiewanie wyjątku Get-WmiObject i testowanie wyjątków w Pester działa dobrze. Ale czy istnieje sposób na przetestowanie funkcji Test-Is64Bit i czy Get-WmiObject rzuci wyjątek taki, że test Pestera wpada w haczyk wewnątrz Test-Is64Bit. Nadal dostaję nieudane testy i widzę wyjątki wyrzucane na konsolę. –
Czy używasz -erroraction stop na get-wmiobject w swojej funkcji? Jeśli nie, twój haczyk nigdy nie zostanie przywołany? –
Próbowałem zarówno z, jak i bez zatrzymania -ArrorAction na Get-WmiObject. Ten sam wyjątek jest wyświetlany w konsoli. –