PowerShell Pester - Code Coverage

简介:

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


来个实例看看,豆子直接在上一篇的脚本里面添加了一个switchtest的function,然后测试了其中一个if的功能


Test.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$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语句的还有一个分支我没有测试


wKioL1djhKKAg25VAADEhJ8ZYGY621.png


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

wKioL1djhX2D2nmWAAC5exxYP4o607.png






本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1790262,如需转载请自行联系原作者

目录
相关文章
|
5月前
解决IDEA提交代码时提示“Performing Code Analysis...”
解决IDEA提交代码时提示“Performing Code Analysis...”
164 0
clion中cpp文件显示This file does not belong to any project ,code insight features might not work【解决方案】
clion中cpp文件显示This file does not belong to any project ,code insight features might not work【解决方案】
clion中cpp文件显示This file does not belong to any project ,code insight features might not work【解决方案】
|
11月前
|
Python
python离线安装环境 解决 ERROR: Could not find a version that satisfies the requirement xxx 以及winError[10061]
python离线安装环境 解决 ERROR: Could not find a version that satisfies the requirement xxx 以及winError[10061]
494 0
|
数据可视化 Shell C++
VS Code|你知道VS Code的命令行操作吗
VS Code|你知道VS Code的命令行操作吗
739 0
VS Code|你知道VS Code的命令行操作吗
成功解决ERROR: Command errored out with exit status 1: command: 'f:\program files\python\python36\pyt
成功解决ERROR: Command errored out with exit status 1: command: 'f:\program files\python\python36\pyt
|
前端开发 数据可视化 Linux
VS Code 常用的一些命令
VS Code 常用的一些命令
|
前端开发 程序员
IDEA运行报Command line is too long错误解决办法
IDEA运行报Command line is too long错误解决办法
420 0
IDEA运行报Command line is too long错误解决办法
IDEA出现问题:断点不可用 No executable code found解决方案
IDEA出现问题:断点不可用 No executable code found解决方案
1823 0
IDEA出现问题:断点不可用 No executable code found解决方案
Visual Studio Code 保存代码时报Applying code action Organize Imports
Visual Studio Code 保存代码时报Applying code action Organize Imports
Visual Studio Code 保存代码时报Applying code action Organize Imports