您好,登錄后才能下訂單哦!
今天繼續學習Pester,invoke-pester有一個很nb的選項叫codeCoverage,可以指定需要測試的腳本,function甚至某一個片段的范圍,然后他會告訴你這個范圍內的功能是否都測試過了。
來個實例看看,豆子直接在上一篇的腳本里面添加了一個switchtest的function,然后測試了其中一個if的功能
Test.ps1
function add { param( [int]$a, [int]$b ) $sum=$a+$b $sum } function switchtest{ param( [switch]$switch ) if($switch){ return "Switch is On" } else{ return "Switch is Off" } }
Test.tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "Test" { Context "Should be test"{ It "Add 1 and 2 is equal to 3" { add 1 2 | Should Be 3 } It "Add -1 and 2 is not equal to 0" { add -1 2 | Should not Be 0 } It "Test Switch option"{ switchtest -switch | Should be "Switch is on" } } Context "Should BeExactly test"{ It "HostName" { hostname | Should beexactly "yli-ise" } } Context "Should BeGreaterThan test"{ It "PsVersion is above 3" { $PSVersionTable.PSVersion.Major | Should beGreaterThan 3 } } Context "Should beOfType test"{ It "Get-ADUser type"{ Get-aduser yli | Should beoftype Microsoft.ActiveDirectory.Management.ADUser } } Context "Should Exist test"{ It "C:\temp exist"{ "c:\temp" | should exist } } Context "Should match test"{ It "Find Email"{ "jksjsjsjssdjs abc.xyz@yahoo.com hsosofs" | should match "[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" } } Context "Should Throw test" { It "Get a non-exist Process"{ {Get-Process -Name "!@#$%&" -ErrorAction Stop} | Should Throw } } Context "Should BeNullorEmpty test"{ It "Get something from test folder"{ get-childitem C:\temp | should not benullorempty } } }
執行看看,最后他告訴我在我指定的腳本里面,只完成了80%的測試,因為if語句的還有一個分支我沒有測試
改變一下腳本的范圍,只測試16-18行的內容,那么最后報告表示選擇范圍的功能已經100%測試過了
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。