PowerShell Pester-code Coverage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.