Point Pester test file at different servers

Hi,

so i have created a file full of pester tests, but at the start i had to hard code the server to connect to.

So when i run the script it always points at that server. Is there a way to pass the server to the pester test file dynamically?

ie can you do something like:

Invoke-Pester “$testsFolder*.ps1” -ServerName “myServer”

or even

Invoke-Pester “$testsFolder*.ps1” -ServerName “myServer”, “myServer2”

Yep. You can put a param block in your tests.ps1 file, same as any other PowerShell script. When you run Invoke-Pester, you can tell it what parameters to pass to the test file like this (assuming you have a parameter named “ExampleParam” that’s expecting a string):

Invoke-Pester -Script @{ Path = '.\SomeFile.Tests.ps1'; Parameters = @{ ExampleParam = 'Value to pass' } }

Hi Dave,

Thanks for the reply!

Any idea what im doing wrong with the below (guessing i’ve over simplified?), tried to make the simplest test to check im connecting to a passed through server.

Calling script:

Invoke-Pester -Script @{Path = “$testsFolder\Parameter.test.ps1”; Parameters = @{$ServerName = ‘MyServerName’}}

Paramter.test.ps1

Param (
[string]$ServerName
)
$Session = New-PSSession -ComputerName $ServerName

Import-Module sqlserver

Describe “test parameter in pester” {

    It "Test Server name" {
        Invoke-Command -Session $Session {$env:COMPUTERNAME} |  Should be 'MyServerName'
    }

}

Im getting angry replies from powershell not liking where im taking this!

You’ve got one extra $ symbol in there. Should look like this:

@{Path = “$testsFolder\Parameter.test.ps1”; Parameters = @{ServerName = ‘MyServerName’}}

(I removed the $ before ServerName)

ha, awesome! thank you sir!