powershell - 在 Powershell 中处理数组(使用 Pester 进行测试)
问题描述
我在理解实现此过程的方式时遇到了一些麻烦。我想在分数中获得总计数,以便如果测试成功通过或失败,可以将其添加到数组中。该数组将计入长度。
这是我的代码作为示例:
#This stores the array of the number of passed and failed test
$passed = @()
$failed = @()
Describe "Template Syntax" {
It "Has a JSON template" {
$fileLocation = "$here\azuredeploy.json"
$fileCheck = $fileLocation | Test-Path
if ($fileCheck -eq $true) { $passed = $passed + 1
Write-Host "1st file exist " }
if ($fileCheck -eq $false) { $failed = $failed + 1
Write-Host "1st file does exist" }
}
It "Has a parameters file" {
$fileLocation ="$here\azuredeploy.parameters*.json"
$fileCheck = $fileLocation | Test-Path
if ($fileCheck -eq $true) { $passed = $passed + 1;
Write-Host "2nd file exist "}
if ($fileCheck -eq $false) { $failed = $failed + 1
Write-Host "2nd file does exist" }
}
PrintArgs
}
function PrintArgs(){
Write-Host -ForegroundColor yellow "Passed: $($passed.Length) Failed: $($failed.Length)"
}
我可以采取其他方式或其他方法来实现这一目标吗?我知道 pester 会自动执行此操作,但是,我想使用 Powershell 脚本进行测试。
解决方案
查看您的代码,您不需要数组来计算分数。不用将$passed
and定义$failed
为数组,只需将它们设置为起始值为 0 的整数计数器
$passed = $failed = 0
然后,您只需调用函数 PrintArgs()而不是
Write-Host -ForegroundColor yellow "Passed: $passed Failed: $failed"
顺便说一句,要增加一个计数器,你可以简单地做$passed++
而不是$passed = $passed + 1
如果您坚持使用数组,您可以$passed = $passed + 1
将$passed += $true
. 通过这样做,您可以向数组添加一个值为 $true 的新元素(或者您认为更合适的任何值。
推荐阅读
- ios - 将图像上传到 Firebase 存储时尝试从对象 [1] 中插入 nil 对象
- c# - 应用程序将数据发送到网页
- c - 当我写 str1 它正在写一个不同的形状
- django - Django get url variables within get_context data?
- firebase - 我应该在我的数据库中存储设备特定的文件系统路径吗?
- sql - 使用嵌套对象更新 SQL Json 字段中的属性
- json - 用户登录时如何显示我的 api 响应?
- code-generation - Simulink 代码生成:来自 Function Caller 模块的函数存根及其返回值/参数
- python - 参数替换后让sqlite3进行查询优化
- python - Asyncio.sleep 阻塞了函数的其余部分