PowerShellPester-CodeCoverage-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
PowerShellPester-CodeCoverage-创新互联

今天继续学习Pester,invoke-pester有一个很nb的选项叫codeCoverage,可以指定需要测试的脚本,function甚至某一个片段的范围,然后他会告诉你这个范围内的功能是否都测试过了。

创新互联建站专注于企业成都营销网站建设、网站重做改版、宝山网站定制设计、自适应品牌网站建设、html5成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为宝山等各大城市提供网站开发制作服务。

来个实例看看,豆子直接在上一篇的脚本里面添加了一个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语句的还有一个分支我没有测试

PowerShell Pester - Code Coverage

改变一下脚本的范围,只测试16-18行的内容,那么最后报告表示选择范围的功能已经100%测试过了

PowerShell Pester - Code Coverage

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:PowerShellPester-CodeCoverage-创新互联
标题链接:http://scgulin.cn/article/doeopd.html