4

Trying to learn Pester (v5.0.4 and PS v7.0.3). I have these files

Get-Planet2.ps1:

function Get-Planet ([string]$Name = '*') {
    $planets = @(
        @{ Name = 'Mercury' }
        @{ Name = 'Venus'   }
        @{ Name = 'Earth'   }
        @{ Name = 'Mars'    }
        @{ Name = 'Jupiter' }
        @{ Name = 'Saturn'  }
        @{ Name = 'Uranus'  }
        @{ Name = 'Neptune' }
    ) | ForEach-Object { [PSCustomObject] $_ }

    $planets | Where-Object { $_.Name -like $Name }
}

Get-Planet2.Tests.ps1:

BeforeAll { 
    # This will bring the function from the main file back to scope.
    . $PSScriptRoot/Get-Planet2.ps1

    param(
        [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$name,
        [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$title,
        [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$InputFile
    )
}

Describe 'Get-Planet' {
    It 'Given no parameters, it lists all 8 planets' {
        $allPlanets = Get-Planet
        $allPlanets.Count | Should -Be 8
    }

    It 'Earth is the third planet in our Solar System' {
        $allPlanets = Get-Planet
        $allPlanets[2].Name | Should -Be 'Earth'
    }

    It 'Pluto is not part of our Solar System' {
        $allPlanets = Get-Planet
        $plutos = $allPlanets | Where-Object Name -EQ 'Pluto'
        $plutos.Count | Should -Be 0
    }

    It 'Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune' {
        $allPlanets = Get-Planet
        $planetsInOrder = $allPlanets.Name -join ', ' 
        $planetsInOrder | Should -Be 'Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune'
    } 

}

Call-Get-Planet2.ps1:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$path = "$here/Get-Planet2.Tests.ps1"
$parameters = @{name = "John"; title = "Sales"; InputFile = "$here/test.json"}

write-host "Path: $path"
write-host $parameters

Invoke-Pester -Script @{Path=$path;Parameters=$parameters}

My Problem:

When I run Call-Get-Planet2.ps1, I ended with the below error. I can't figure out why it is doing this. Need guidance. Thanks

System.Management.Automation.RuntimeException: No test files were found and no scriptblocks were provided. at Invoke-Pester, /Users/myaccount/.local/share/powershell/Modules/Pester/5.0.4/Pester.psm1: line 4520 at , /Users/myaccount/repos/myrepo/Pester/Call-Get-Planet2.ps1: line 8 at , : line 1

Ultra GC
  • 311
  • 4
  • 15
  • What studies have you reviewed before diving into this? For example [pester beginning](https://www.youtube.com/results?search_query=pester+beginning) – postanote Oct 18 '20 at 08:20
  • I went thru the Pester tutorial. My files are mainly from the tutorial. I made a changes by adding the parameters to the tests.ps1 file. When I try to add invoke-pester -script (with parameters), it failed with the mentioned error. Been doing more research, it may be because the the now v5.0.4 is not working well with parameters. – Ultra GC Oct 18 '20 at 17:44
  • 1
    @UltraGC - Starting from scratch: what happens when you cd into the folder containing the tests file and simply `Invoke-Pester .`? – Lieven Keersmaekers Oct 19 '20 at 05:52
  • @LievenKeersmaekers Thanks for the suggestions. I get "The term 'param' is not recognized as the name of a cmdlet,.." when just running invoke-pester. I moved the "param" section (in Get-Planet2.Tests.ps1 ) of of before-all section but didn't help much. In 5.0.4, it could be that the "parameters" is ignored or no longer requred? – Ultra GC Oct 19 '20 at 16:27
  • Not sure but I think you need to wrap everything in a function. Can you wrap a `function Get-Planet-Tests {` ... `}` around your testcode? – Lieven Keersmaekers Oct 19 '20 at 17:39
  • They changed how you call Invoke-Pester in version 5. Check out the information about the Configuration object here: https://github.com/pester/Pester#simple-and-advanced-interface If you are using a tutorial pre-v5 there are some significant differences so you'll want to read through that. Reply if you run into any other issues and I'll try to help out. – Efie Oct 20 '20 at 19:04
  • Thanks for the info. My tutorial is pre-v5. Will need to re-visit this when I have more time as other priorities have came up. – Ultra GC Oct 23 '20 at 03:53

2 Answers2

1

If you encounter this error ('No test files were found and no scriptblocks were provided'), you should follow LievenKeersmaekers' suggestion (made in the question comments), which is to execute the test file more directly by doing something like the following:

cd {TO\A\FOLDER\WITH\A\TEST_FILE}
Invoke-Pester -Path .\{TEST_FILE}.ps1

You'll likely see a more descriptive error message that will help you to solve your issue.

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
0

Pester v5 had a breaking change and removed support for Invoke-Pester -Script <hashtable>. Pester 5.1 re-introduced support for script parameters using containers which you create with New-PesterContainer -Path <file> -Data <parametersHashtable> and invoke using Invoke-Pester -Container ....

For the original demo, upgrade to Pester 5.1 or later and modify:

  • Get-Planet2.Tests.ps1 to:
# Script parameters need to be in the root of the file.
param(
    [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$name,
    [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$title,
    [parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$InputFile
)

BeforeAll { 
    # This will bring the function from the main file back to scope.
    . $PSScriptRoot/Get-Planet2.ps1
}

Describe 'Get-Planet' {
    It 'Given no parameters, it lists all 8 planets' {
        $allPlanets = Get-Planet
        $allPlanets.Count | Should -Be 8
    }

    It 'Earth is the third planet in our Solar System' {
        $allPlanets = Get-Planet
        $allPlanets[2].Name | Should -Be 'Earth'
    }

    It 'Pluto is not part of our Solar System' {
        $allPlanets = Get-Planet
        $plutos = $allPlanets | Where-Object Name -EQ 'Pluto'
        $plutos.Count | Should -Be 0
    }

    It 'Planets have this order: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune' {
        $allPlanets = Get-Planet
        $planetsInOrder = $allPlanets.Name -join ', ' 
        $planetsInOrder | Should -Be 'Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune'
    } 

}
  • Call-Get-Planet2.ps1 to:
# $PSScriptRoot is easier than Split-Path :-)
$here = $PSScriptRoot
$path = "$here/Get-Planet2.Tests.ps1"
$parameters = @{name = 'John'; title = 'Sales'; InputFile = "$here/test.json" }

Write-Host "Path: $path"
Write-Host $parameters

# -Script <hashtable> is not supported in v5. 
# Use New-PesterContainer
$container = New-PesterContainer -Path $path -Data $parameters
Invoke-Pester -Container $container
Frode F.
  • 52,376
  • 9
  • 98
  • 114