溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PowerShell Pester - Code Coverage

發(fā)布時(shí)間:2020-06-15 22:39:37 來源:網(wǎng)絡(luò) 閱讀:740 作者:beanxyz 欄目:開發(fā)技術(shù)

今天繼續(xù)學(xué)習(xí)Pester,invoke-pester有一個(gè)很nb的選項(xiàng)叫codeCoverage,可以指定需要測試的腳本,function甚至某一個(gè)片段的范圍,然后他會(huì)告訴你這個(gè)范圍內(nèi)的功能是否都測試過了。


來個(gè)實(shí)例看看,豆子直接在上一篇的腳本里面添加了一個(gè)switchtest的function,然后測試了其中一個(gè)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
    }
}
}


執(zhí)行看看,最后他告訴我在我指定的腳本里面,只完成了80%的測試,因?yàn)閕f語句的還有一個(gè)分支我沒有測試


PowerShell Pester - Code Coverage


改變一下腳本的范圍,只測試16-18行的內(nèi)容,那么最后報(bào)告表示選擇范圍的功能已經(jīng)100%測試過了

PowerShell Pester - Code Coverage

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI