PowerShell Pester 使用 - Assertion

豆子之前初步了解了Pester的基本功能,今天继续看看。Pester里面有个很重要的概念叫 assertion (断言),他的作用是通过Should这个关键字 (function)来定义预测应该出现的结果。

这个shoud后面的操作符有以下几种

  • Be                      
  • BeExactly          
  • BeGreaterThan  
  • BeLessThan      
  • BeLike                
  • BeLikeExactly     
  • BeOfType           
  • Exist                    
  • Contain               
  • ContainExactly   
  • Match                  
  • MatchExactly      
  • Throw                  
  • BeNullOrEmpty   

下面这个链接有相关的wiki说明,有兴趣的可以看看

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

这些关键字操作符从名字都能猜的出是干啥的,下面给几个实例看看怎么用。

比如在一个test.ps1里面写一个简单求和功能

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

对应的test.tests.ps1 里面这么写

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace ‘\.Tests\.‘, ‘.‘
. "$here\$sut"

Describe "Test" {

#Shoud Be 比较结果是否一样,不区分大小写
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 比较结果是否一样,区分大小写
Context "Should BeExactly test"{
    It "HostName" {
        hostname | Should beexactly "yli-ise"
    }

}

#Should BeGreaterThan判断得到的结果是否比预定值大
Context "Should BeGreaterThan test"{
    It "PsVersion is above 3" {
        $PSVersionTable.PSVersion.Major | Should beGreaterThan 3
    }

}

#Should beoftype判断结果类型是否为指定类型
Context "Should beOfType test"{
    It "Get-ADUser type"{
        Get-aduser yli | Should beoftype Microsoft.ActiveDirectory.Management.ADUser
    }

}

#Should Exist判断文件是否存在
Context "Should Exist test"{
    It "C:\temp exist"{
        "c:\temp" | should exist
    }
     
}

#Should match 判断结果是否匹配正则表达式, 不区分大小写

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])?"
    }
     
}

#Should Throw 判断script block结果是否抛出异常
Context "Should Throw test" {
    It "Get a non-exist Process"{ 
    
        {Get-Process -Name "[email protected]#$%&" -ErrorAction Stop} | Should Throw
    }
}

#Should BeNulorEmpty 判断结果是否为空
Context "Should BeNullorEmpty test"{
    It "Get something from test folder"{
    
        get-childitem C:\temp | should not benullorempty
    }

}

}

直接运行这个测试文件或者通过Invoke-pester执行,看看结果 成功

时间: 2024-08-05 23:40:50

PowerShell Pester 使用 - Assertion的相关文章

PowerShell Pester - Code Coverage

今天继续学习Pester,invoke-pester有一个很nb的选项叫codeCoverage,可以指定需要测试的脚本,function甚至某一个片段的范围,然后他会告诉你这个范围内的功能是否都测试过了. 来个实例看看,豆子直接在上一篇的脚本里面添加了一个switchtest的function,然后测试了其中一个if的功能 Test.ps1 function add { param( [int]$a, [int]$b ) $sum=$a+$b $sum } function switchtes

Unit Testing PowerShell Code with Pester

Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerShell code. Note   This is a five-part series that includes the following posts: What is Pester and Why Should I Care?Learn about a new test framework

PowerShell Script Analyzer, Script browser 和 Pester

昨天在MVA上看PowerShell5的最新功能的合集视频,第一个演讲人是微软PowerShell 开发组的经理,他提到了DevOp 的发展趋势,他认为对于PowerShell而言,除了基本的PowerShell的技能,还需要掌握以下基本的技能和工具: PowerShell DSC 版本控制 Git & Github 脚本最佳优化 Script Analyzer 单元测试 Pester PowerShell DSC和Github 豆子倒是用过,后面两个是什么呢? 首先看看Script Analy

CentOS 7 上面安装PowerShell

看了文章 爱上PowerShell , 就想在CentOS 7上面试试PowerShell , 本文记录了在CentOS 7.2上安装Powershell 的过程. 首先我们要从github上下载最新的PowerShell 的rpm 包powershell-6.0.0_alpha.14-1.el7.centos.x86_64.rpm,安装方法的文档 https://github.com/PowerShell/PowerShell/blob/master/docs/installation/lin

【Windows10 IoT开发系列】PowerShell的相关配置

原文:[Windows10 IoT开发系列]PowerShell的相关配置 可使用 Windows PowerShell 远程配置和管理任何 Windows 10 IoT 核心版设备.PowerShell 是基于任务的命令行 Shell 和脚本语言,专为进行系统管理而设计. 1.​启动 PowerShell (PS) 会话 注:若要使用装有Windows10 IoT Core设备启动PS会话,首先需要在主机电脑与设备之间创建信任关系. ​启动 Windows IoT 核心版设备后,与该设备相连的

【Windows10 IoT开发系列】Powershell命令行实用程序

原文:[Windows10 IoT开发系列]Powershell命令行实用程序 更新帐户密码: 强烈建议你更新默认的管理员帐户密码.若要更新帐户密码,你可以发出以下命令: net user Administrator [new password]​ (其中 [new password] 表示你选择的强密码). 创建本地用户帐户: 如果你想要授予其他人访问你的 Windows IoT Core 设备的权限,你可以通过在 net user [username] [password] /add​ 中键

浅谈Ubuntu PowerShell——小白入门教程

早在去年八月份PowerShell就开始开源跨平台了,但是一直没有去尝试,叫做PowerShell Core. 这里打算简单介绍一下如何安装和简单使用,为还不知道PowerShell Core on Ubuntu的同学们提供一点小小的入门帮助,谢谢大家支持~ PowerShell Core是由Microsoft开发的运行在.Net Core上的开源跨平台的任务自动化和配置管理系统. 1.   在Ubuntu 16.04上安装PowerShell Core a)         导入公共存储库GP

(17)Powershell中的重定向运算符

默认情况下,Powershell 把输出发送到屏幕显示.但是,Powershell也可以将输出重定向至一个文本文件,或将错误输出重定向至常规输出流. 重定向运算符有什么用 ? 重定向运算符意味着我们可以将命令的输出信息输出到指定的文件,完全满足脚本中的log的要求,即可以利用重定向打印脚本或命令执行的详细信息. Powershell 中有以下重定向运算符. 运算符 说明 示例 > 将输出发送到指定文件. PS D:\> Get-Process > process.txt >>

(18)Powershell中的字符串拆分运算符

Powershell中提供了对字符串的拆分操作运算符.-split 运算符将一个字符串拆分成多个字符串. 拆分运算符 拆分运算符用于将一个或多个字符串拆分为多个子字符串.可更改拆分操作的以下元素: (1)定界符.默认为空白,但是可指定字符.字符串.模式或用于设置定界符的脚本块. (2)子字符串的最大数目.默认设置为返回所有子字符串.如果指定的数字小于子字符串数,则其余子字符串将合并到最后一个子字符串中. (3)用于指定定界符匹配条件的选项,如 SimpleMatch 和 Multiline. 拆