1

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.

2 Answers2

0

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.

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thank you so much for the useful help :) , but i am still having trouble with my output. At the end of my result, i should get Passed = 2, Failed = 0. Although I get Passed=0 and Failed=0. @Theo – Donald Duck Jul 23 '18 at 08:04
  • Are you now using simple counters instead of arrays or?? – Theo Jul 23 '18 at 08:29
  • Yep i have changed it into a counter :) @Theo – Donald Duck Jul 23 '18 at 08:40
  • Ah.. Sorry i made a stupid typo. Edited my answer. What i meant was to drop the `printArgs` function and write out the counters straight away. Anyway.. Marks answer is better, but this may have helped in understanding increments and adding elements to arrays I hope. – Theo Jul 23 '18 at 20:54
0

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.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68