Point Pester test file at different servers

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of staggerlee011 staggerlee011 3 months, 1 week ago.

  • Author
    Posts
  • #51426
    Profile photo of staggerlee011
    staggerlee011
    Participant

    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"

    • This topic was modified 3 months, 2 weeks ago by Profile photo of staggerlee011 staggerlee011.
  • #51449
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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' } }
    
  • #51459
    Profile photo of staggerlee011
    staggerlee011
    Participant

    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!

  • #51461
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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)

  • #51464
    Profile photo of staggerlee011
    staggerlee011
    Participant

    ha, awesome! thank you sir!

You must be logged in to reply to this topic.