简体   繁体   中英

Dealing with Arrays in Powershell (testing with Pester)

I have a little trouble understanding the way to implement this process. I want to achieve a total count in the score so that if a test successfully passes or fails it can be added into an array. That array will be counted in the length.

This is my code as an example:

#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)"
   }

Is there a different way or another approach that I can do to achieve this? I know that pester does it automatically, however, I want to use a Powershell script to test.

Looking at your code, you do not need arrays to count the scores. Instead of defining $passed and $failed as arrays, just set them up as integer counters with a starting value of 0

$passed = $failed = 0

Then instead of calling function PrintArgs() you simply do

Write-Host -ForegroundColor yellow "Passed: $passed Failed: $failed"

By the way, to increment a counter you can simply do $passed++ instead of $passed = $passed + 1

If you DO insist on using arrays you can change the $passed = $passed + 1 to something like $passed += $true . By doing that you add a new element to the array with a value of $true (or whatever you feel is more appropriate.

Your Pester tests aren't really Pester tests unless you include a Should assertion. I think you should rewrite your tests as follows:

Describe "Template Syntax" {
    $fileLocation = "$here\azuredeploy.json" 
    $fileCheck = $fileLocation | Test-Path

    It "Has a JSON template" {        
        $fileCheck | Should -Be $true
    }

    $fileLocation ="$here\azuredeploy.parameters*.json"
    $fileCheck = $fileLocation | Test-Path

    It "Has a parameters file" {        
        $fileCheck | Should -Be $true
    } 
}

If you then run this with Invoke-Pester you get a summary of passed and failed test counts at the end automatically. If you need to access these values, you can use -PassThru to return them to a variable. For example:

$Results = Invoke-Pester .\your.tests.ps1 -PassThru

Then you can get the number of passed and failed tests as follows:

$Results.PassedCount
$Results.FailedCount

If you genuinely want to use your own counters (which would mean maintaining lots of unnecessary logic) you could do as follows:

$Passed = 0
$Failed = 0

Describe "Template Syntax" {
    $fileLocation = "$here\azuredeploy.json" 
    $fileCheck = $fileLocation | Test-Path

    It "Has a JSON template" {        
        $fileCheck | Should -Be $true
    }

    if ($fileCheck -eq $true) {  $Passed++ } else { $Failed++ }

    $fileLocation ="$here\azuredeploy.parameters*.json"
    $fileCheck = $fileLocation | Test-Path

    It "Has a parameters file" {        
        $fileCheck | Should -Be $true
    } 

    if ($fileCheck -eq $true) {  $Passed++ } else { $Failed++ }

    Write-Host -ForegroundColor yellow "Passed: $Passed Failed: $Failed"
}

Note that the Describe block acts as a kind of script scope, so you have to print the values of $Passed and $Failed within the Describe to get at the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM