PowerShell Pester Use-assertion

Source: Internet
Author: User

Beans before the preliminary understanding of the basic functions of pester, today continue to look. A very important concept in pester is called assertion (assertion), and his role is to define the results of predictions that should occur by should the keyword (function).

There are several operators behind this shoud

  • Be

  • beexactly

  • Begreaterthan

  • Belessthan

  • Belike

  • belikeexactly

  • Beoftype

  • Exist

  • Contain

  • containexactly

  • Match

  • matchexactly

  • Throw

  • Benullorempty


The following link has the relevant wiki description, interested to see

Https://github.com/pester/Pester/wiki/Should



These key operators can guess from the name of what is what to do, here are a few examples to see how to use.

For example, write a simple sum function in a TEST.PS1


function add {param ([int] $a, [int] $b) $sum = $a + $b $sum}


The corresponding TEST.TESTS.PS1 is written inside this

$here  = Split-Path -Parent  $MyInvocation .mycommand.path$sut =  (split-path - leaf  $MyInvocation. Mycommand.path)  -replace  ' \. Tests\. ',  '.   "$here \ $sut" describe  "Test"  {#Shoud  Be  Compare results, case-insensitive 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    }} #should  be Exactly  Compare whether the results are the same, case-sensitive context  "should beexactly test" {    it  "HostName"  {         hostname | should beexactly  "Yli-ise"     } #Should   Begreaterthan judged whether the results were greater than the predetermined value context  "Should begreaterthan test" {    it  "Psversion is above 3"  {        $ psversiontable.psversion.major | should begreaterthan 3    }} #Should  beoftype determines whether the result type is a specified type context  "Should beoftype test" {    it  " Get-aduser type "{        get-aduser yli | should  beoftype microsoft.activedirectory.management.aduser    }} #Should   Exist determine if the file exists context  "should exist test" {    it  "C:\temp exist" {          "C:\Temp"  | should exist     }    &NBSP,} #Should  match  determine if the result matches a regular expression, &nbsp, case-insensitive 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])? "     }    &NBSP,} #Should  Throw  Judge script  Whether the block result throws an exception context  "Should throw test"  {    It  "Get a  non-exist process "{             { get-process -name  "[email protected]#$%&"  -erroraction stop} | should  THROW   &NBSP}} #Should  BeNulorEmpty  determine if the result is empty context  "should  Benullorempty test "{    it " get something from tEst folder "{            get-childitem c : \TEMP | SHOULD NOT BENULLOREMPTY    }}}


Run the test file directly or execute it through Invoke-pester to see the results succeed

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/D5/wKioL1diJY2h_F-OAAB0bHDsNck914.png "title=" 8.PNG " alt= "Wkiol1dijy2h_f-oaab0bhdsnck914.png"/>

This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1789850

PowerShell Pester Use-assertion

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.