Continue learning today Pester,invoke-pester has a very NB option called Codecoverage, which can specify the script to be tested, function, or even the range of a fragment, and then he will tell you whether the functionality in this range has been tested.
Let's take a look at the beans. A switchtest function was added to the previous script, and then one of the if functions was tested
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 [email protected] 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 "[email protected]#$%&" - erroraction stop} | should throw }}context "Should Benullorempty test "{ it " Get something from test folder "{ get-childitem c:\temp | should not benullorempty }}}
Execution look, and finally he told me that in the script I specified, only 80% of the tests were done, because there was another branch of the IF statement I didn't test.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/DF/wKioL1djhKKAg25VAADEhJ8ZYGY621.png "title=" 9.PNG " alt= "Wkiol1djhkkag25vaadehj8zygy621.png"/>
Change the scope of the script to test only 16-18 rows of content, then the final report indicates that the function of the selection has been tested 100%
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/DF/wKioL1djhX2D2nmWAAC5exxYP4o607.png "title=" 10.PNG "alt=" Wkiol1djhx2d2nmwaac5exxyp4o607.png "/>
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1790262
PowerShell Pester-code Coverage